JAVA:从字符串中获取 UTF-8 十六进制值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9706782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:28:25  来源:igfitidea点击:

JAVA: get UTF-8 Hex values from a string?

javautf-8hexconverter

提问by thedp

I would like to be able to convert a raw UTF-8 string to a Hex string. In the example below I've created a sample UTF-8 string containing 2 letters. Then I'm trying to get the Hex values but it gives me negative values.

我希望能够将原始 UTF-8 字符串转换为十六进制字符串。在下面的示例中,我创建了一个包含 2 个字母的示例 UTF-8 字符串。然后我试图获得十六进制值,但它给了我负值。

How can I make it give me 05D0and 05D1

我怎样才能让它给我05D005D1

String a = "\u05D0\u05D1";
byte[] xxx = a.getBytes("UTF-8");

for (byte x : xxx) {
   System.out.println(Integer.toHexString(x));
}

Thank you.

谢谢你。

采纳答案by ataylor

Don't convert to an encoding like UTF-8 if you want the code point. Use Character.codePointAt.

如果需要代码点,请不要转换为 UTF-8 之类的编码。使用Character.codePointAt

For example:

例如:

Character.codePointAt("\u05D0\u05D1", 0) // returns 1488, or 0x5d0

回答by Malcolm

Negative values occur because the range of byteis from -128 to 127. The following code will produce positive values:

出现负值byte是因为 的范围是从 -128 到 127。以下代码将产生正值:

String a = "\u05D0\u05D1";
byte[] xxx = a.getBytes("UTF-8");

for (byte x : xxx) {
    System.out.println(Integer.toHexString(x & 0xFF));
}

The main difference is that it outputs x & 0xFFinstead of just x, this operation converts byteto int, dropping the sign.

主要区别在于它输出x & 0xFF而不是仅输出x,此操作转换byteint,丢弃符号。