字符串“8000000000000000”(16 字节)相当于 Java 中的 "BCD"(8 字节)

String "8000000000000000" (16 bytes) to equivalent "BCD" (8 bytes) in Java

我编写了自己的函数,将一个字符串转换为 BCD 格式的等效字节 []。然后我将这些字节发送到 DataOutputStram(使用需要字节 [] 数组的写入方法)。问题在于数字字符串“8000000000000000”。看这个:

First two characters: "8" "0"
"8" = "1000"
"0" = "0000"
binary value of the first byte = "10000000"
Exception in thread "Thread-3" java.lang.NumberFormatException: Value out of range. Value:"10001000" Radix:2

你看到问题了吗?我正在尝试将“128”十进制值保存在字节 [] 中,但这是不可能的,因为字节范围值介于 -128 和 127 之间。我能做什么?

这是我的函数代码:

public class UtilityBCD 
{
    //Decimal:    0     1     2     3     4     5     6     7     8     9
    //BCD:     0000  0001  0010  0011  0100  0101  0110  0111  1000  1001

    private static final String zero = "0000";
    private static final String one = "0001";
    private static final String two = "0010";
    private static final String three = "0011";
    private static final String four = "0100";
    private static final String five = "0101";
    private static final String six = "0110";
    private static final String seven = "0111";
    private static final String eight = "1000";
    private static final String nine = "1001";

    public static byte[] numericStringToBCD(String value)
    {
        int len = value.length();
        String values[] = new String[len];

        //WE CREATE AN ARRAY WITH THE BCD VALUE OF EACH CHARACTER
        for ( int i = 0; i < len; i++ )
        {
            values[i] = toBCDValue(value.charAt(i));
            System.out.println(values[i]);
        }
        System.out.println("\nEnd of values\n");

        //WE DETERMINATE IF THE STRING IS ODD AND WE CREATE
        //THE NEW ARRAY WITH THE HELF OF SIZE OF THE ORIGINAL
        //STRING
        int iterator;
        boolean isOdd = len % 2 != 0;
        iterator = len/2;

        String values2[] = new String[iterator];
        System.out.println("ITERATOR: " + iterator);
        //WE SET THE NEW ARRAY WITH THE COUPLES OF BCD'S VALUES
        int j = 0;
        for ( int i = 0; i < iterator;i ++)
        {
            if ( isOdd && i == (iterator - 1) )
                values2[i] = values[j];
            else
            {
                values2[i] = values[j] + values[j + 1];
                j++;
            }
        }

        //FINALLY WE CREATE AN ARRAY OF BYTE'S AND SAVE THE EACH
        byte values3[] = new byte[iterator];
        for ( int i = 0; i < iterator; i++ )
        {
            System.out.println("DEBUG BCD : " +  values2[i]);
            values3[i] = Byte.parseByte(values2[i], 2); //HERE IS THE PROBLEM
        }

        return values3;
    }

    private static String toBCDValue(char character)
    {
        String bcdValue = null;

        switch(character)
        {
            case '0': bcdValue = zero; break;
            case '1': bcdValue = one; break;
            case '2': bcdValue = two; break;
            case '3': bcdValue = three; break;
            case '4': bcdValue = four; break;
            case '5': bcdValue = five; break;
            case '6': bcdValue = six; break;
            case '7': bcdValue = seven; break;
            case '8': bcdValue = eight; break;
            case '9': bcdValue = nine; break;
        }

        return bcdValue;
    }
}

此致!

我可以想到几个选项:

  • 使用除 Byte 之外的可以保存此类值的类型(例如 Integer)。
  • 将您的值转换为可以存储在 Byte 中的值。假设你的值在 0 到 255 之间,减去 128 将把它放在一个允许的范围内(即使用 Byte.parseByte(values2[i], 2)-128)。根据您的应用程序,您可能需要一些其他转换。

好的,我找到了解决方案,我简单地更改了这一行:

values3[i] = Byte.parseByte(values2[i], 2); //HERE IS THE PROBLEM

现在这一项和所有工作都完美了!

values3[i] = (byte) Integer.parseInt(values2[i], 2);

如果有人想解释一下,我将不胜感激。

此致!