I need to convert a number to byte array and then back to number. The problem is that the byte array is of variable size, so I need to convert a number given his byte length, the methods that I came up with are those: (Java)
我需要将一个数字转换为字节数组,然后再转换为数字。问题是字节数组的大小是可变的,所以我需要转换给定字节长度的数字,我想出的方法是:( Java)
private static byte[] toArray(long value, int bytes) {
byte[] res = new byte[bytes];
final int max = bytes*8;
for(int i = 1; i <= bytes; i++)
res[i - 1] = (byte) (value >> (max - 8 * i));
return res;
}
private static long toLong(byte[] value) {
long res = 0;
for (byte b : value)
res = (res << 8) | (b & 0xff);
return res;
}
Here I use a long because 8 is the max bytes we can use. This method works perfectly with positive numbers but I can't seem to make the decoding work with negatives.
这里我使用long,因为8是我们可以使用的最大字节数。这种方法与正数完美匹配,但我似乎无法使解码与负数一起工作。
EDIT: to test this I've tried with processing the value Integer.MIN_VALUE + 1 (-2147483647) and 4 bytes
编辑:测试这个我试过处理值Integer.MIN_VALUE + 1(-2147483647)和4个字节
2 个解决方案
#1
1
After accepting this as working solution, the Asker made some further optimizations.
I have included their ownlinked code
below for reference :在接受这个作为工作解决方案后,Asker进行了一些进一步的优化。我在下面提供了他们自己的链接代码供参考:
private static long toLong(byte[] value) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
final byte val = (byte) (value[0] < 0 ? 0xFF : 0);
for(int i = value.length; i < Long.BYTES; i++)
buffer.put(val);
buffer.put(value);
return buffer.getLong(0);
}
OLDER ANSWER
edit : Based on comments (understanding Question better)
编辑:基于评论(更好地理解问题)
To make your toLong
function handle both negative & positive numbers try this:
要使您的toLong函数同时处理负数和正数,请尝试以下方法:
private static long toLong(byte[] value)
{
long res = 0;
int tempInt = 0;
String tempStr = ""; //holds temp string Hex values
tempStr = bytesToHex(value);
if (value[0] < 0 )
{
tempInt = value.length;
for (int i=tempInt; i<8; i++) { tempStr = ("FF" + tempStr); }
res = Long.parseUnsignedLong(tempStr, 16);
}
else { res = Long.parseLong(tempStr, 16); }
return res;
}
Below is related bytesToHex
function (re-factored to work out-of-box with any byte[]
input...)
下面是相关的bytesToHex函数(重新计算到任何byte []输入的开箱即用...)
public static String bytesToHex(byte[] bytes)
{ String tempStr = ""; tempStr = DatatypeConverter.printHexBinary(bytes); return tempStr; }
#2
1
Take a look at Apache Common Conversion.intToByteArray util method.
看一下Apache Common Conversion.intToByteArray util方法。
JavaDoc:
Converts a int into an array of byte using the default (little endian, Lsb0) byte and bit ordering
使用默认(little endian,Lsb0)字节和位排序将int转换为byte数组
#1
1
After accepting this as working solution, the Asker made some further optimizations.
I have included their ownlinked code
below for reference :在接受这个作为工作解决方案后,Asker进行了一些进一步的优化。我在下面提供了他们自己的链接代码供参考:
private static long toLong(byte[] value) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
final byte val = (byte) (value[0] < 0 ? 0xFF : 0);
for(int i = value.length; i < Long.BYTES; i++)
buffer.put(val);
buffer.put(value);
return buffer.getLong(0);
}
OLDER ANSWER
edit : Based on comments (understanding Question better)
编辑:基于评论(更好地理解问题)
To make your toLong
function handle both negative & positive numbers try this:
要使您的toLong函数同时处理负数和正数,请尝试以下方法:
private static long toLong(byte[] value)
{
long res = 0;
int tempInt = 0;
String tempStr = ""; //holds temp string Hex values
tempStr = bytesToHex(value);
if (value[0] < 0 )
{
tempInt = value.length;
for (int i=tempInt; i<8; i++) { tempStr = ("FF" + tempStr); }
res = Long.parseUnsignedLong(tempStr, 16);
}
else { res = Long.parseLong(tempStr, 16); }
return res;
}
Below is related bytesToHex
function (re-factored to work out-of-box with any byte[]
input...)
下面是相关的bytesToHex函数(重新计算到任何byte []输入的开箱即用...)
public static String bytesToHex(byte[] bytes)
{ String tempStr = ""; tempStr = DatatypeConverter.printHexBinary(bytes); return tempStr; }
#2
1
Take a look at Apache Common Conversion.intToByteArray util method.
看一下Apache Common Conversion.intToByteArray util方法。
JavaDoc:
Converts a int into an array of byte using the default (little endian, Lsb0) byte and bit ordering
使用默认(little endian,Lsb0)字节和位排序将int转换为byte数组