-1

I need to create a string of hex values. Now, i've got something like this.

String address = "5a f2 ff 1f";

But when getting this address into bytes:

byte[] bytes= address.getBytes();

It gets me each letter and space as a byte, instead of getting each 2 chars as a byte ang leaving the spaces. So...

How can i declare this?

    private String CalcChecksum (String message) {

    /**Get string's bytes*/
    message = message.replaceAll("\\s","");
    byte[] bytes = toByteArray(message);
    byte b_checksum = 0;

    for (int byte_index = 0; byte_index < byte_calc.length; byte_index++) {
        b_checksum += byte_calc[byte_index];
    }

    int d_checksum = b_checksum;  //Convert byte to int(2 byte)
    int c2_checksum = 256 - d_checksum;  
    String hexString = Integer.toHexString(c2_checksum);  

    return hexString;
}
masmic
  • 3,526
  • 11
  • 52
  • 105

2 Answers2

1
String address = "5a f2 ff 1f";
byte[] bytes = DatatypeConverter.parseHexBinary(address.replaceAll("\\s","")).getBytes();

As stated you're using hex, which you cannot use .getBytes() in the way you are trying to!

Joe Birch
  • 371
  • 2
  • 7
1

You need to specify that your string contains hex values. And in the solution below you need to remove all whitespaces from the string before converting it:

import javax.xml.bind.DatatypeConverter;

class HexStringToByteArray {

    public static void main(String[] args) {
        String address = "5A F2 FF 1F"; 
        address = address.replaceAll("\\s","");
        System.out.println(address);
        byte[] bytes = toByteArray(address);
        for( byte b: bytes ) {
            System.out.println(b);
        }
        String string_again =  toHexString(bytes);
        System.out.println(string_again);
    }
    public static String toHexString(byte[] array) {
        return DatatypeConverter.printHexBinary(array);
    }

    public static byte[] toByteArray(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }

}

This will print (note that bytes as signed):

5AF2FF1F    // Original address
90          // 5A
-14         // F2
-1          // FF
31          // 1F
5AF2FF1F    // address retrieved from byte array
keyser
  • 18,829
  • 16
  • 59
  • 101
  • Great function. But need to implement the code in a Checksum function, so i need some more code to be implemented. I'm editting the post to insert the Checksum function where I need to implement this. – masmic Aug 05 '13 at 14:47
  • I've got a error whith toByteArray. `The method is undefined` – masmic Aug 05 '13 at 14:50
  • @masmic_87 As you can see I defined it myself. – keyser Aug 05 '13 at 14:54
  • Yeah, you're right. But i'm still having problems with the DatatypeConverter, the import shows: The import javax.xml.bind cannot be resolved. – masmic Aug 06 '13 at 07:57
  • @masmic_87 That error is extremely common. Google it. [Here's](http://stackoverflow.com/a/4240351/645270) one idea. – keyser Aug 06 '13 at 10:02
  • Yes, solved this error. But I still need some help. Doing this, it gets the entire hex string to convert into bytes. But what I need is to convert each byte one by one. – masmic Aug 06 '13 at 14:25
  • @masmic_87 Why would you need to do that? I included an example of how to loop through the array. – keyser Aug 06 '13 at 16:33
  • Keyser, its Ok. I've seen this [post](http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java) and what I say it is done there inside the One-liner. What i'm trying to accomplish is to get the same checksum that one partner has made on the MCU's software that I have to access. I've been trying with several functios but I always get something different to what he has. But I've realized that when getting the hex to byte array que is getting it like unsigned while I'm getting it like signed. So for the moment i think this is the solution. – masmic Aug 07 '13 at 07:19
  • @masmic_87 Yes, I noted that the bytes were signed, sorry if I wasn't clear about the fact that that means that the printed value isn't the hex value. – keyser Aug 07 '13 at 09:26