将ASCII表情转换为十六进制

huangapple go评论139阅读模式
英文:

Convert ASCII emoji to hexadecimal

问题

关于大部分情况下,将ASCII转换为十六进制的操作可以通过以下方式进行:

Hex.encodeHex(ascii.getBytes(StandardCharsets.UTF_8))

然而,这似乎并不完全正确。我尝试将“微笑脸”(🙂)的ASCII转换为十六进制,但得到的结果是“c3b0c5b8e284a2e2809a”,虽然是正确的,但又不正确。我正在寻找的值是“F09F9982”,当将ASCII放入文件中并执行“od -x”命令时可以轻松找到这个值。甚至可以在Notepad++中实现。

我无法弄清楚的是,当我尝试编写Java等效代码时到底做错了什么。我甚至尝试了将String.getBytes转换为UTF-8,然后将其转换为UTF-16来构建字符串,但仍然无法获得正确的结果。我的结果仍然是“c3b0c5b8e284a2e2809a”。

有人知道如何生成正确的值吗?

我相当确定我在UTF-8和UTF-16转换之间做了一些愚蠢的事情,但由于某种原因我无法弄清楚。任何帮助将不胜感激。

英文:

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.

答案1

得分: 1

好的,以下是您要翻译的内容:

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
英文:

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

huangapple
  • 本文由 发表于 2020年8月27日 12:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63609073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定