在 Java 中从十六进制字符串创建 ISO-8859-1 字符串,移位
我正在尝试将十六进制序列转换为以 ISO-8859-1、UTF-8 或 UTF-16BE 编码的字符串。也就是说,我有一个字符串,看起来像:“0422043504410442”
,它代表UTF-16BE中的字符:“Test”
。
我用来在两种格式之间进行转换的代码是:
private static String hex2String(String hex, String encoding) throws UnsupportedEncodingException {
char[] hexArray = hex.toCharArray();
int length = hex.length() / 2;
byte[] rawData = new byte[length];
for(int i=0; i<length; i++){
int high = Character.digit(hexArray[i*2], 16);
int low = Character.digit(hexArray[i*2+1], 16);
int value = (high << 4) | low;
if( value > 127)
value -= 256;
rawData[i] = (byte) value;
}
return new String(rawData, encoding);
}
这似乎对我来说工作得很好,但我仍然有两个问题:
- 是否有更简单的方法(最好没有位处理)来进行此转换?
- 我该如何解释这一行:
int value = (high << 4) |低;
?
我熟悉位处理的基础知识,但完全不熟悉 Java 语法。我相信第一部分将所有位向左移动 4 步。尽管其余的我不明白,也不明白为什么它在这种特定情况下会有所帮助。
对于我的问题中的任何困惑,我深表歉意,如果我需要澄清任何事情,请告诉我。 谢谢。 //Abeansits
I am trying to convert a HEX-sequence to a String encoded in either, ISO-8859-1, UTF-8 or UTF-16BE. That is, I have a String looking like: "0422043504410442"
this represents the characters: "Test"
in UTF-16BE.
The code I used to convert between the two formats was:
private static String hex2String(String hex, String encoding) throws UnsupportedEncodingException {
char[] hexArray = hex.toCharArray();
int length = hex.length() / 2;
byte[] rawData = new byte[length];
for(int i=0; i<length; i++){
int high = Character.digit(hexArray[i*2], 16);
int low = Character.digit(hexArray[i*2+1], 16);
int value = (high << 4) | low;
if( value > 127)
value -= 256;
rawData[i] = (byte) value;
}
return new String(rawData, encoding);
}
This seems to work fine for me, but I still have two questions regarding this:
- Is there any simpler way (preferably without bit-handling) to do this conversion?
- How am I to interpret the line:
int value = (high << 4) | low;
?
I am familiar with the basics of bit-handling, though not at all with the Java syntax. I believe the first part shift all bits to the left by 4 steps. Though the rest I don't understand and why it would be helpful in this certain situation.
I apologize for any confusion in my question, please let me know if I should clarify anything.
Thank you.
//Abeansits
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有没有更简单的方法(最好没有位处理)来进行这种转换?
我不知道 - 唯一的简化似乎是一次解析整个字节而不是逐位解析(例如使用
int value = Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
)我如何解释这一行: int value = (high < ;<4)| low;?
查看此示例,了解最后两位数字 (42):
Is there any simpler way (preferably without bit-handling) to do this conversion?
None I would know of - the only simplification seems to parse the whole byte at once rather than parsing digit by digit (e.g. using
int value = Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
)How am I to interpret the line: int value = (high << 4) | low;?
look at this example for your last two digits (42):
在这种情况下,您可以将
<<
和|
替换为*
和+
,但我不这样做不推荐它。该表达式
相当于
不需要减去 256 来得到 -128 和 127 之间的值。例如,简单地将 128 转换为一个字节将产生正确的结果。
int
128 的最低 8 位与byte
-128:0x80 具有相同的模式。我会把它简单地写成:
You can replace the
<<
and|
in this case with*
and+
, but I don't recommend it.The expression
is equivalent to
The subtraction of 256 to get a value between -128 and 127 is unnecessary. Simply casting, for example, 128 to a byte will produce the correct result. The lowest 8 bits of the
int
128 have the same pattern as thebyte
-128: 0x80.I'd write it simply as:
您可以使用 Hex 类在 Apache commons 中,但在内部,它会做同样的事情,也许有细微的差别。
这将两个十六进制数字(每个数字代表 4 位)组合成一个无符号 8 位值,存储为
int
。接下来的两行将其转换为有符号的 Java字节
。You can use the Hex class in Apache commons, but internally, it will do the same thing, perhaps with minor differences.
This combines two hex digits, each of which represents 4 bits, into one unsigned 8-bit value stored as an
int
. The next two lines convert this to a signed Javabyte
.