如何在Java中将int数据转换为压缩十进制

时间:2019-06-07 15:09:00

标签: java data-conversion dataformat

我正在处理Java代码,我需要将int数据转换为Java中的压缩十进制。 因为压缩十进制是一种旧格式(COBOL)。我发现的解决方案将converting int处理为压缩十进制,但不是浮点型。 我有两个变量,长度和小数点后有多少个数字必须存储为压缩十进制。

例如:import networkx as nx print(nx.__version__) import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(211) ax2 = fig.add_subplot(212) G = nx.graph_atlas(1) plt.sca(ax1) nx.draw(G, ax=ax1) plt.show()

这意味着length = 6(in half a byte) 存储小数点后的半字节数= 2

total length=3 bytes+1/5 byte+1/5 byte(To get even number of bytes)

以防半字节数= 4

(Unsigned) 12345.67 >> 1234567F

任何代码都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

这是@GilbertLeBlanc提供的示例from this post,应该可以解决您的问题。

public class PackedDecimal {

    public static byte[] format(long number, int bytes) {
        byte[] b = new byte[bytes];

        final byte minusSign = 0x0D; // Minus
        final byte noSign = 0x0F; // Unsigned

        String s = Long.toString(number);
        int length = s.length();
        boolean isNegative = false;

        if (s.charAt(0) == '-') {
            isNegative = true;
            s = s.substring(1);
            length--;
        }

        int extraBytes = length - bytes + 1;

        if (extraBytes < 0) {
            // Pad extra byte positions with zero
            for (int i = 0; i < -extraBytes; i++) {
                b[i] = 0x00;
            }
        } else if (extraBytes > 0) {
            // Truncate the high order digits of the number to fit
            s = s.substring(extraBytes);
            length -= extraBytes;
            extraBytes = 0;
        }

        // Translate the string digits into bytes
        for (int i = 0; i < length; i++) {
            String digit = s.substring(i, i + 1);
            b[i - extraBytes] = Byte.valueOf(digit);
        }

        // Add the sign byte
        if (isNegative) {
            b[bytes - 1] = minusSign;
        } else {
            b[bytes - 1] = noSign;
        }

        return b;
    }

    public static void main(String[] args) {
        long number = -456L;
        byte[] b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));

        number = 0L;
        b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));

        number = 5823L;
        b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));

        number = 123456L;
        b = PackedDecimal.format(number, 5);
        System.out.println("Number: " + number + ", packed: " + byteToString(b));
    }

    public static String byteToString(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            sb.append("0x");
            sb.append(Integer.toHexString((int) b[i]).toUpperCase());
            sb.append(" ");
        }
        return sb.toString();
    }

}

主要方法的测试结果如下:

Number: -456, packed: 0x0 0x4 0x5 0x6 0xD 
Number: 0, packed: 0x0 0x0 0x0 0x0 0xF 
Number: 5823, packed: 0x5 0x8 0x2 0x3 0xF 
Number: 123456, packed: 0x3 0x4 0x5 0x6 0xF