Java (android) 中的 Little 和 Big Endian
Little and Big Endian in Java (android)
我正在使用 Android Studio 构建应用程序,在我的项目中,我需要进行大量转换,例如 short/int 到字节数组。我还希望我的应用程序从用 C 编码的机器人接收数据,并且机器人发送一个包含很多 uint16-32、int16-32 的结构....
我找到了很多帮助我在 bytearray 中转换属性的帖子和代码,但我总是看到人们在谈论 Little 和 Big Endian,我无法理解其中的区别。如果有人可以向我解释......
注意:机器人通过 TCP 协议的 Wifi 套接字发送数据
在将整数转换为字节流时,通常会将整数分成字节,一个字节一个字节地发送。如果发送的第一个字节包含最低有效位,则为小端字节序。如果发送的第一个字节包含最高有效位,则为大端字节序。在小尾数法中,1 将由字节 1 后跟一个包含 0 的字节表示。(由于字节可以由两个十六进制数字表示,因此通常以这种方式表示。)所以短整数 1 被转换为字节 01 00短整数 256 成为小端字节序中的字节 00 01。在big endian中,1的字节流是00 01,256变成01 00.
Little Endian 和 Big Endian 只是指数据结构的 bytes 出现的顺序。
想象一下,您有一个由十六进制值 0xabcd 表示的 16 位整数。因为 8 位 = 1 字节,所以我们的整数由两个字节组成,ab 和 cd。在Big Endian系统中,最高有效字节放在低位内存地址,而在Little Endian系统中,我们将它们放在高位。
为了直观地显示这一点,假设我们将整数放在内存地址 0 处。
在 Big Endian 系统中,我们的内存看起来像这样:
Memory address -> | 0 | 1 |
Value -> | ab | cd |
在 Little Endian 系统中,它看起来像这样:
Memory address -> | 0 | 1 |
Value -> | cd | ab |
传统上,网络字节顺序是 Big Endian。
String path=”root/subdir/filename.extension”;
File f=new File(path);
RandomAccessFile raf=new RandomAccessFile(f,”r”);
ByteBuffer bb;
char c;
int i;
ch=raf.readChar();
bb=ByteBuffer.allocate(4); //4 byte buffer
bb.order(ByteOrder.BIG_ENDIAN);
bb.putChar(ch); //store 2 byte unsigned value in byte buffer and reach 2
position forward //in buffer
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.position(0); //goto start of buffer again for reading
ch=bb.getChar(); //retrieve 2 byte unsigned value from byte buffer
i=(int) ch; //always store a 2-byte unsigned value (e.g.unsigned char) into
a 4-byte signed //datatype to avoid storing as 2’s compliment negative no.
//now 4 byte integer ‘i’ contains a 2-byte unsigned +ve integer value for
processing
System.out.println(“2 byte unsigned int value=”,i );
我正在使用 Android Studio 构建应用程序,在我的项目中,我需要进行大量转换,例如 short/int 到字节数组。我还希望我的应用程序从用 C 编码的机器人接收数据,并且机器人发送一个包含很多 uint16-32、int16-32 的结构.... 我找到了很多帮助我在 bytearray 中转换属性的帖子和代码,但我总是看到人们在谈论 Little 和 Big Endian,我无法理解其中的区别。如果有人可以向我解释...... 注意:机器人通过 TCP 协议的 Wifi 套接字发送数据
在将整数转换为字节流时,通常会将整数分成字节,一个字节一个字节地发送。如果发送的第一个字节包含最低有效位,则为小端字节序。如果发送的第一个字节包含最高有效位,则为大端字节序。在小尾数法中,1 将由字节 1 后跟一个包含 0 的字节表示。(由于字节可以由两个十六进制数字表示,因此通常以这种方式表示。)所以短整数 1 被转换为字节 01 00短整数 256 成为小端字节序中的字节 00 01。在big endian中,1的字节流是00 01,256变成01 00.
Little Endian 和 Big Endian 只是指数据结构的 bytes 出现的顺序。
想象一下,您有一个由十六进制值 0xabcd 表示的 16 位整数。因为 8 位 = 1 字节,所以我们的整数由两个字节组成,ab 和 cd。在Big Endian系统中,最高有效字节放在低位内存地址,而在Little Endian系统中,我们将它们放在高位。
为了直观地显示这一点,假设我们将整数放在内存地址 0 处。
在 Big Endian 系统中,我们的内存看起来像这样:
Memory address -> | 0 | 1 |
Value -> | ab | cd |
在 Little Endian 系统中,它看起来像这样:
Memory address -> | 0 | 1 |
Value -> | cd | ab |
传统上,网络字节顺序是 Big Endian。
String path=”root/subdir/filename.extension”;
File f=new File(path);
RandomAccessFile raf=new RandomAccessFile(f,”r”);
ByteBuffer bb;
char c;
int i;
ch=raf.readChar();
bb=ByteBuffer.allocate(4); //4 byte buffer
bb.order(ByteOrder.BIG_ENDIAN);
bb.putChar(ch); //store 2 byte unsigned value in byte buffer and reach 2
position forward //in buffer
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.position(0); //goto start of buffer again for reading
ch=bb.getChar(); //retrieve 2 byte unsigned value from byte buffer
i=(int) ch; //always store a 2-byte unsigned value (e.g.unsigned char) into
a 4-byte signed //datatype to avoid storing as 2’s compliment negative no.
//now 4 byte integer ‘i’ contains a 2-byte unsigned +ve integer value for
processing
System.out.println(“2 byte unsigned int value=”,i );