将字节数组块转换为DWORD(int)Java
如何将4字节数组块转换为整数 如果我们在十六进制编辑器中有这些字节的文件:将字节数组块转换为DWORD(int)Java,java,decimal,block,endianness,Java,Decimal,Block,Endianness,如何将4字节数组块转换为整数 如果我们在十六进制编辑器中有这些字节的文件: CB 01 00 00 此块的十六进制值是十进制的DWORD,单位为000001CB=1CBhex=459 如何将任何字节数组(byte[])块(四个字节)转换为整数(块的十进制值) 我正在寻找这样的方法: public int getDecimalFromBlock( byte... bytes ) { for ( byte b: bytes ) { // do the magic
CB 01 00 00
此块的十六进制值是十进制的DWORD,单位为000001CB
=1CB
hex=459
如何将任何字节数组(byte[]
)块(四个字节)转换为整数(块的十进制值)
我正在寻找这样的方法:
public int getDecimalFromBlock( byte... bytes ) {
for ( byte b: bytes ) {
// do the magic
}
}
其中,参数byte在字节范围(-127127)之间。假设您的字节顺序是Little Endian(基于您的post),因此解码将是
int value = ((data[0]&0xff) |
((data[1]&0xff)<<8) |
((data[2]&0xff)<<16) |
((data[3]&0xff)<<24));
更新已编辑的请求。
我假设您可能在解释为负值的0xcb方面有问题,这可以通过应用&
运算符来解决
用测试用例完成代码
public class DecimalTest
{
public static int getDecimalFromBlock( byte... bytes ) {
int result = 0;
for(int i=0; i<bytes.length; i++)
{
result = result | (bytes[i] & 0xff)<<(i*8);
}
return result;
}
public static void main(String[] args) throws IOException
{
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01, 0x00}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01, 0x00, 0x00}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01, 0x00, 0x00, 0x00}));
}
}
公共类小数
{
公共静态整型getDecimalFromBlock(字节…字节){
int结果=0;
对于(int i=0;i让我们假设您的字节顺序是Little Endian(基于您的post),因此解码将是
int value = ((data[0]&0xff) |
((data[1]&0xff)<<8) |
((data[2]&0xff)<<16) |
((data[3]&0xff)<<24));
更新已编辑的请求。
我假设您可能在解释为负值的0xcb方面有问题,这可以通过应用&
运算符来解决
用测试用例完成代码
public class DecimalTest
{
public static int getDecimalFromBlock( byte... bytes ) {
int result = 0;
for(int i=0; i<bytes.length; i++)
{
result = result | (bytes[i] & 0xff)<<(i*8);
}
return result;
}
public static void main(String[] args) throws IOException
{
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01, 0x00}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01, 0x00, 0x00}));
System.out.println(getDecimalFromBlock(new byte[]{(byte)0xcb, 0x01, 0x00, 0x00, 0x00}));
}
}
公共类小数
{
公共静态整型getDecimalFromBlock(字节…字节){
int结果=0;
对于(inti=0;i,您还可以查看ApacheCommonsIO库及其类
请注意,四字节DWORD(无符号)只能正确映射到Java(有符号)long
,因为Java(有符号)int
不能包含DWORD的所有可能值(值太大会导致负int
值)。因此EndianUtils.readSwappedUnsignedInteger()
正确地返回一个长的
您还可以查看Apache Commons IO库及其类。它正是为了这个目的
请注意,四字节DWORD(无符号)只能正确映射到Java(有符号)long
,因为Java(有符号)int
不能包含DWORD的所有可能值(值太大会导致负int
值)。因此EndianUtils.readSwappedUnsignedInteger()
正确返回long
将其读入ByteBuffer,将字节缓冲区的顺序设置为LITTLE_ENDIAN,并调用ByteBuffer的getInt方法。将其读入ByteBuffer,将字节缓冲区的顺序设置为LITTLE_ENDIAN,并调用ByteBuffer的getInt方法。如何将其设置为方法?更新了我的问题。编辑:您有id吗ea如何做它向后?从整数到字节块?我有问题转换十六进制“FE”(dec 254)以匹配字节范围…如何将其放入方法?更新了我的问题。编辑:你知道如何做它向后?从整数到字节块?我有问题转换十六进制“FE”(dec 254)以匹配字节范围。。。