[英]Is there a better way to convert byte array to an int?
The first 3 bytes of a byte array are just integers, is there a better way to convert them?字节数组的前 3 个字节只是整数,有没有更好的方法来转换它们? So far I have this but it just feels like a bad way of doing it.到目前为止,我有这个,但它只是感觉这样做是一种糟糕的方式。
public int parse_code(byte[] bs) {
char[] array = new char[3];
for(int i = 0; i < 3; i++) {
array[i] = (char) bs[i];
}
// Dirty way of doing it
return Integer.parseInt(new String(array));
}
If you know that there will always be 3 decimal digits at the beginning of the byte array you can just convert them into a integer directly:如果您知道字节数组的开头总会有 3 个十进制数字,您可以直接将它们转换为 integer:
public int parse_code( byte[] bs )
{
int intval = 0;
for( int i = 0; i < 3; i++ )
intval = intval * 10 + ( bs[ i ] - '0' );
return intval;
}
Your example code seems to associate the byte
s with their ASCII value.您的示例代码似乎将byte
s 与其 ASCII 值相关联。 The proper way to do that would be to use the String
constructor that takes the byte
array and a character set:正确的方法是使用接受byte
数组和字符集的String
构造函数:
// note: throws charset exception that will never be thrown on a valid JVM
// as all JVMs must support US-ASCII
Integer.parseInt(new String(byteArray, "US-ASCII"));
Note : If the byte
s were not ASCII character values representing integers (eg, it's just an int
encoded as four byte
s), then you would want to look at the ByteBuffer
class.注意:如果byte
s 不是表示整数的 ASCII 字符值(例如,它只是一个编码为四个byte
s 的int
),那么您可能需要查看ByteBuffer
class。 It has helpers that can convert from the ByteBuffer
into other buffers (eg,IntBuffer
) to enable simple allow looping if it's all one type (as opposed to a mixed message, such as an incoming C-struct or something).它具有可以从ByteBuffer
转换为其他缓冲区(例如IntBuffer
)的助手,以启用简单的允许循环,如果它都是一种类型(而不是混合消息,例如传入的 C-struct 或其他东西)。 It also has the added perk of enabling endianness to be changed.它还有一个额外的好处是可以改变字节顺序。
int bytesValue = ByteBuffer.wrap(byteArray).getInt();
It's also worth noting, as x4u pointed out, that ByteBuffer
does require the proper number of byte
s for each value it gets.同样值得注意的是,正如 x4u 所指出的那样, ByteBuffer
确实需要为它获得的每个值提供适当数量的byte
。 So, the above getInt()
method will use the next 4 byte
s and fail (with an exception) if there are [0, 3].因此,上面的getInt()
方法将使用接下来的 4 byte
,如果有 [0, 3] 则失败(有一个例外)。
If you want to convert it in to int, the size of the array should be max 4 and the int not to exceed 214783647. Then:如果要将其转换为 int,则数组的大小应最大为 4,并且 int 不超过 214783647。然后:
int i = (bs[3] << 24) + (bs[2] << 16) + (bs[1] << 8) + bs[0];
Other things you have to know: which position in the byte array where it fits in the int (big-endian vs little-endian): (instead the previous expression) maybe you need:你必须知道的其他事情:字节数组中的 which position 适合 int (大端与小端):(而不是前面的表达式)也许你需要:
int i = (bs[0] << 24) + (bs[1] << 16) + (bs[2] << 8) + bs[3];
if not all 4 bytes present you have to check the length.如果不是所有 4 个字节都存在,则必须检查长度。
It looks like you are trying to convert three byte of ASCII text like 123
as [49, 50, 51]
to 123.看起来您正在尝试将三个字节的 ASCII 文本(如123
)转换为[49, 50, 51]
到 123。
public static long parse_code(byte... bs) {
long value = 0;
for(byte b: bs)
value = value * 10 + b - '0';
return value;
}
if you know it will always be 3 bytes.如果您知道它将始终是 3 个字节。
public static int parse_code(byte... bs) {
return bs[0]*100 + bs[1]*10 + bs[2] - '0' * 111;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.