For the most part, conversion of ascii to hex works with this.
Hex.encodeHex(ascii.getBytes(StandardCharsets.UTF_8))
However, this doesn't appear to be exactly correct. I was trying to convert "slightly smiling face" ascii (🙂) into hex and keep getting "c3b0c5b8e284a2e2809a" which is correct but not correct. The value I'm looking for is "F09F9982" which is easily figured out when doing an "od -x" when the ascii is put in a file. You can even do it in Notepad++.
What I can't figure out is what the heck I'm doing wrong when trying to code a Java equivalent. I even tried String.getBytes to UTF-8 and then take that to build a string to UTF-16 and still couldn't get the right results. I keep getting "c3b0c5b8e284a2e2809a" for my result.
Anybody know how to generate the correct value?
I'm pretty sure I'm doing something dumb between UTF-8 and UTF-16 conversion but I can't figure it out for some reason. Any help is appreciated.
The character set you are expecting is Windows-1252, not UTF-8.
byte[] bytes = "🙂".getBytes(Charset.forName("windows-1252"));
for (byte b : bytes)
System.out.printf("%02X", b & 0xff);
output:
F09F9982