0

I made my own function to convernt one String to its equivalent bytes[] in BCD. Then i send this bytes to and DataOutputStram (with write method that requires an byte[] array). The problem is with the numeric String "8000000000000000". Look this:

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

Did you see the problem? I am trying to save "128" decimal value in a byte[], but it is not possible becouse the byte range values is between -128 and 127. What can i do?

This is the code of my function:

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;
    }
}

Regards!

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
Nico
  • 374
  • 2
  • 4
  • 17
  • As a note, `10000000` is considered -128 in two's complement for bytes. – Compass Jan 23 '15 at 19:55
  • 1
    What is the significance of `8000000000000000`? If that is a number, that overflows a 32 bit number and you'll get weird results. – ryanyuyu Jan 23 '15 at 20:30
  • It does not matter. It is a simple numeric string that i need to transform to a byte[8] in BCD. Maybe the question is can i work with a "unsigned byte" in java? – Nico Jan 23 '15 at 20:36
  • Ok if I understand correctly, you are trying to compress a string representation of 16 nybbles like `8 0 0 0 0 0 ...` into 8 bytes like `80 00 ...` – ryanyuyu Jan 23 '15 at 20:41
  • Is "8000000000000000" supposed to be decimal or hex or what? If it's hex then that's more than 16 decimal digits and hence larger than can be expressed with 8 bytes of BCD. – Hot Licks Jan 23 '15 at 20:43

2 Answers2

1

Ok, i found the solution, i simple changes this line:

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

For this one and all work perfect now!

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

If anyone wants to explain this i will be grateful.

Regards!

Nico
  • 374
  • 2
  • 4
  • 17
0

I can think of a couple of options:

  • Use a type other than Byte that can hold such a value (Integer, for example).
  • Convert your values to ones that can be stored in a Byte. Assuming your values are between 0 and 255, subtracting 128 will put it in an allowable range (that is, use Byte.parseByte(values2[i], 2)-128). Depending on your application, you might want some other transform.
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Hello @Scott Hunter, i need to send a byte[] beocuse i am using a DataOutputStream.write(byte[]) to send the information. I don't understand your second suggest, can you give me an example? – Nico Jan 23 '15 at 19:37