英文:
How to convert signed byte array to ascii in Java
问题
以下是翻译好的内容:
在Java程序中,我有一个已签名的字节数组,内容如下:
[-112, 21, 64, 0, 7, 50, 54, 127]
我如何将其转换为与以下值相等的ASCII数字:
901540000732367F
英文:
In Java program
I have signed byte array as
[-112, 21, 64, 0, 7, 50, 54, 127]
how i can convert into ascii number
which is equal to
901540000732367F
答案1
得分: 1
好的,以下是翻译好的内容:
似乎结果中字节的顺序与数组的顺序相反,因此您应该以相反的顺序迭代数组,并且将每个元素与预定义的位数移位相加:
private static String convertToHexFullByte(byte[] arr) {
return convertToHex(arr, 8);
}
private static String convertToHexHalfByte(byte[] arr) {
return convertToHex(arr, 4);
}
private static String convertToHex(byte[] arr, int bits) {
long mask = 0;
for (int i = 0; i < bits; i++) {
mask |= 1 << i;
}
long res = 0;
for (int i = arr.length - 1, j = 0; i >= 0; i--, j += bits) {
res |= (arr[i] & mask) << j;
}
return Long.toHexString(res).toUpperCase();
}
测试
public static void main(String args[]) {
byte[] arr4 = {49, 55, 48, 51, 55};
System.out.println(convertToHexHalfByte(arr4));
byte[] arr8 = {-112, 21, 64, 0, 7, 50, 54, 127};
System.out.println(convertToHexFullByte(arr8));
}
输出
17037
901540000732367F
英文:
It seems that the order of bytes in the result is reverse to that of the array, so you should iterate the array in the reverse order and add each element with a shift by the predefined number of bits:
private static String convertToHexFullByte(byte[] arr) {
return convertToHex(arr, 8);
}
private static String convertToHexHalfByte(byte[] arr) {
return convertToHex(arr, 4);
}
private static String convertToHex(byte[] arr, int bits) {
long mask = 0;
for (int i = 0; i < bits; i++) {
mask |= 1 << i;
}
long res = 0;
for (int i = arr.length - 1, j = 0; i >= 0; i--, j += bits) {
res |= (arr[i] & mask) << j;
}
return Long.toHexString(res).toUpperCase();
}
Test
public static void main(String args[]) {
byte[] arr4 = {49, 55, 48, 51, 55};
System.out.println(convertToHexHalfByte(arr4));
byte[] arr8 = {-112, 21, 64, 0, 7, 50, 54, 127};
System.out.println(convertToHexFullByte(arr8));
}
output
17037
901540000732367F
答案2
得分: 1
尝试一下这个。它的工作原理是:
- 通过流式传输字节数组的索引
- 映射到 int 并且去掉符号扩展
- 通过位移和或操作将其缩减为 long。
byte[] bytes = { -112, 21, 64, 0, 7, 50, 54, 127 };
long lng = IntStream.range(0, bytes.length)
.mapToLong(i -> bytes[i] & 0xff)
.reduce(0L, (a, b) -> (a << 8) | b);
System.out.println("long decimal value = " + lng);
System.out.println("long hex value = " + Long.toHexString(lng));
输出结果为:
long decimal value = -8064469188872096129
long hex value = 901540000732367f
使用相同的技术,另一个例子 {49, 55, 48, 51, 55}
应该是:
long decimal value = 211379303223
long hex value = 3137303337
英文:
Try this. It works by:
- streaming the indices of the byte array
- maping to an int and getting rid of the sign extension
- reducing to a long by shift and or operations.
byte[] bytes = { -112, 21, 64, 0, 7, 50, 54, 127 };
long lng = IntStream.range(0, bytes.length)
.mapToLong(i -> bytes[i] & 0xff)
.reduce(0L, (a, b) -> (a << 8) | b);
System.out.println("long decimal value = " + lng);
System.out.println("long hex value = " + Long.toHexString(lng));
prints
long decimal value = -8064469188872096129
long hex value = 901540000732367f
Using the same technique, the other example of {49, 55, 48, 51, 55}
should be:
long decimal value = 211379303223
long hex value = 3137303337
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论