Reputation: 167
I'm building an app using Android Studio and in my project, I need to do a lot of conversions like short/int to byte arrays. I also want my app to receive data from a robot which is coded in C, and the robot sends a structure which has a lot of uint16-32, int16-32.... I found a lot of posts and codes that helped me convert my attributes in bytearray but I always see people talking about Little and Big Endian and i can't grasp the difference. If someone could explain it to me .... NB :the robot sends data through a Wifi socket with TCP protocol
Upvotes: 4
Views: 4498
Reputation: 43
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 );
for full code see here: program to read and store big and little endian
Upvotes: 0
Reputation: 331
Little Endian and Big Endian simply refer to the order in which the bytes of a data structure are presented.
Imagine for a moment that you have a 16-bit integer that is represented by the hexidecimal value 0xabcd. Because 8 bits = 1 byte, we have our integer is composed of two bytes, ab and cd. In a Big Endian system, the most significant bytes are placed in the lower memory address, while in Little Endian systems, we put them in the higher one.
To show this visually, assume we've put our integer at the memory address 0.
In a Big Endian system, our memory would look like this:
Memory address -> | 0 | 1 |
Value -> | ab | cd |
In a Little Endian system, it would look like this:
Memory address -> | 0 | 1 |
Value -> | cd | ab |
Traditionally, network byte order is Big Endian.
Upvotes: 5
Reputation: 455
When converting an integer to a byte stream, the integer is usually divided into bytes and the bytes are sent one by one. If the first byte sent contains the least significant bits, it is little endian. If the first byte sent contains the most significant bits, it is big endian. In little endian, 1 would be represented by the bytes 1 followed by a byte containing 0. (Since bytes can be represented by two hexadecimal digits, they are often represented this way.) So the short integer 1 is converted into the bytes 01 00 and the short integer 256 becomes the bytes 00 01 in little endian. In big endian, the byte stream for 1 is 00 01 and 256 becomes 01 00.
See https://en.wikipedia.org/wiki/Endianness
Upvotes: 2