atiqkhaled
atiqkhaled

Reputation: 386

UTF-16 conversion given wrong hex value

Code snippet is :

String str = "h";
StringBuffer buf = new StringBuffer();
byte[] bytes = str.getBytes("UTF-16BE");
for (int i = 0; i < bytes.length; i++) {
    String byteAsHex = Integer.toHexString(bytes[i]);
    buf.append(byteAsHex);
}
System.out.println(buf.toString());

Output is : 068 where LATIN SMALL LETTER H is 0068.

could you please tell me why leading 0 is missing?

Upvotes: -1

Views: 514

Answers (2)

Thomas Fritsch
Thomas Fritsch

Reputation: 10127

It is because Integer.toHexString(0) results in "0", but not "00".

You can fix this issue when you replace

Integer.toHexString(bytes[i])

by

String.format("%02x", bytes[i])

Upvotes: 3

Mike Nakis
Mike Nakis

Reputation: 61993

This is happening because Integer.toHexString() will always return the shortest possible representation of a number, that is, without any leading zeros. So, in your case, you have an array of 2 bytes: [0, 0x68] and Integer.toHexString() is invoked twice, the first time it returns 0, and the second time it returns 68.

In order to solve this problem you are going to need to prepend '0' to each string returned by Integer.toHexString() if the string length is 1.

Upvotes: 2

Related Questions