[英]Creating a ISO-8859-1 string from a HEX-string in Java, shifting bits
I am trying to convert a HEX-sequence to a String encoded in either, ISO-8859-1, UTF-8 or UTF-16BE. 我正在尝试将HEX序列转换为以ISO-8859-1,UTF-8或UTF-16BE编码的字符串。 That is, I have a String looking like: "0422043504410442"
this represents the characters: "Test"
in UTF-16BE. 也就是说,我有一个字符串看起来像: "0422043504410442"
这代表字符:UTF-16BE中的"Test"
。
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: 这似乎对我很好,但我仍有两个问题:
int value = (high << 4) | low;
我怎么解释这一行: int value = (high << 4) | low;
int value = (high << 4) | low;
? ? I am familiar with the basics of bit-handling, though not at all with the Java syntax. 我熟悉位处理的基础知识,尽管Java语法完全没有。 I believe the first part shift all bits to the left by 4 steps. 我相信第一部分将所有位向左移动4步。 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 // Abeansits
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 (eg using int value = Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
) 没有我会知道 - 唯一的简化似乎是一次解析整个字节而不是逐位解析(例如使用int value = Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
)
public static byte[] hexToBytes(final String hex) {
final byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return bytes;
}
How am I to interpret the line: int value = (high << 4) | 我怎么解释这一行:int value =(high << 4)| low;? 低;?
look at this example for your last two digits (42): 看看这个例子中的最后两位数字(42):
int high = 4; // binary 0100
int low = 2; // binary 0010
int value = (high << 4) | low;
int value = (0100 << 4) | 0010; // shift 4 to left
int value = 01000000 | 0010; // bitwise or
int value = 01000010;
int value = 66; // 01000010 == 0x42 == 66
You can replace the <<
and |
你可以替换<<
和|
in this case with *
and +
, but I don't recommend it. 在这种情况下使用*
和+
,但我不推荐它。
The expression 表达方式
int value = (high << 4) | low;
is equivalent to 相当于
int value = high * 16 + low;
The subtraction of 256 to get a value between -128 and 127 is unnecessary. 减去256以获得-128和127之间的值是不必要的。 Simply casting, for example, 128 to a byte will produce the correct result. 例如,简单地将128转换为一个字节将产生正确的结果。 The lowest 8 bits of the int
128 have the same pattern as the byte
-128: 0x80. int
128的最低8位具有与byte
-128:0x80相同的模式。
I'd write it simply as: 我把它写成:
rawData[i] = (byte) ((high << 4) | low);
Is there any simpler way (preferably without bit-handling) to do this conversion? 有没有更简单的方法(最好没有位处理)来进行这种转换?
You can use the Hex class in Apache commons, but internally, it will do the same thing, perhaps with minor differences. 你可以在Apache commons中使用Hex类,但在内部,它会做同样的事情,也许会有细微的差别。
How am I to interpret the line:
int value = (high << 4) | low;
我怎么解释这一行:int value = (high << 4) | low;
int value = (high << 4) | low;
? ?
This combines two hex digits, each of which represents 4 bits, into one unsigned 8-bit value stored as an int
. 它将两个十六进制数字组合成一个存储为int
无符号8位值,每个十六进制数字代表4位。 The next two lines convert this to a signed Java byte
. 接下来的两行将其转换为带符号的Java byte
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.