Java - 将Big-Endian转换为Little-Endian [英] Java - Convert Big-Endian to Little-Endian
问题描述
我有以下十六进制字符串:
00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81
$
$ b
81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000(Big
endian)
我认为我必须反转并交换字符串,但是像这样的东西并没有给我正确的结果:
字符串十六进制=00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81;
hex = new StringBuilder(hex).reverse()。toString();
lock $
$ b 81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000
(应该是)
交换:
public static String hexSwap(String origHex){
//从十六进制数中创建数字
BigInteger orig = new BigInteger(origHex,16);
//获取交换字节
byte [] origBytes = orig.toByteArray();
int i = 0;
while(origBytes [i] == 0)i ++;
//交换字节
byte [] swapBytes = new byte [origBytes.length]; ($ / $ / $; i< origBytes.length; i ++){
swapBytes [i] = origBytes [origBytes.length-i-1];
}
BigInteger swap = new BigInteger(swapBytes);
return swap.toString(10);
}
hex = hexSwap(hex);
结果:
026053973026883595670517176393898043396144045912271014791797784
(错误)
81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000
(应该是)
任何人都可以给我一个如何完成这个任务的例子吗?
非常感谢你)
解决方案您需要交换每个对字符,因为你正在颠倒字节的顺序,而不是nybbles。例如:
public static String reverseHex(String originalHex){
// TODO:验证长度是甚至
int lengthInBytes = originalHex.length()/ 2;
char [] chars = new char [lengthInBytes * 2];
for(int index = 0; index< lengthInBytes; index ++){
int reversedIndex = lengthInBytes - 1 - index;
chars [reversedIndex * 2] = originalHex.charAt(index * 2);
chars [reversedIndex * 2 + 1] = originalHex.charAt(index * 2 + 1);
}
返回新的String(字符);
}
I have the following hex string:
00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81
but I want it to look like this:
81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000 (Big
endian)
I think I have to reverse and swap the string, but something like this doesn't give me right result:
String hex = "00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81";
hex = new StringBuilder(hex).reverse().toString();
Result:
81dc20bae765e9b8dc39712eef992fed444da92b8b58b14a3a80000000000000
(wrong)
81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000
(should be)
The swapping:
public static String hexSwap(String origHex) {
// make a number from the hex
BigInteger orig = new BigInteger(origHex,16);
// get the bytes to swap
byte[] origBytes = orig.toByteArray();
int i = 0;
while(origBytes[i] == 0) i++;
// swap the bytes
byte[] swapBytes = new byte[origBytes.length];
for(/**/; i < origBytes.length; i++) {
swapBytes[i] = origBytes[origBytes.length - i - 1];
}
BigInteger swap = new BigInteger(swapBytes);
return swap.toString(10);
}
hex = hexSwap(hex);
Result:
026053973026883595670517176393898043396144045912271014791797784
(wrong)
81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000
(should be)
Can anyone give me a example of how to accomplish this?
Thank you a lot :)
解决方案 You need to swap each pair of characters, as you're reversing the order of the bytes, not the nybbles. So something like:
public static String reverseHex(String originalHex) {
// TODO: Validation that the length is even
int lengthInBytes = originalHex.length() / 2;
char[] chars = new char[lengthInBytes * 2];
for (int index = 0; index < lengthInBytes; index++) {
int reversedIndex = lengthInBytes - 1 - index;
chars[reversedIndex * 2] = originalHex.charAt(index * 2);
chars[reversedIndex * 2 + 1] = originalHex.charAt(index * 2 + 1);
}
return new String(chars);
}
这篇关于Java - 将Big-Endian转换为Little-Endian的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!