0

I have a byte array in an iOS application,

const uint8 unsignedByteArray[] = {189U, 139U, 64U, 0U};

I need to send these same exact values from an Android device running Java.

Is there a way to reliably convert the unsigned values into something Java can use and send that is equivalent to the byte values on iOS?

pendext
  • 89
  • 2

1 Answers1

1

In Objective-c, create a hex string from the data...

const uint8_t bytes[] = {189U, 139U, 64U, 0U};
NSMutableString *result  = [NSMutableString string];

for (int i = 0; i < 4; ++i) {
    [result appendFormat:@"%02x", (uint8_t)bytes[i]];
}
NSLog(@"%@",result);

This will result in....

bd8b4000

Then apply an idea like this in java (copied the code here verbatim)...

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}
Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136