You can actually follow this process step-by-step in code by using printf("%x", newbyte) to print each of the bytes - the %x format allows you to print in hexadecimal. return newbyte0 | newbyte1 | newbyte2 | newbyte3 Īnd finally you just OR all the bytes to finish the reversal. The same as the first line (this time the redundant mask really is omitted) - the right-most byte becomes the left-most byte. Shifts the number 8 bits to the left this time - essentially the same thing as the last line, only now the second byte from the right becomes the second byte from the left. Shifts the number 8 bits to the right, so that the second byte from the left is now the second byte from the right, and the rest of the bytes are zeroed out with a mask. Utility for byte swapping of all java data types / (C) 2004 - Geotechnical Software Services This code is free software you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation either version 2.1 of the License, or (at your option) any later version. It then uses a mask ( 0xff) to zero out the rest of the bytes, which is redundant as the shift will have zeroed them anyways, so the mask can be omitted. Shifts the number 24 bits to the right, so that the left-most byte will now be the right-most byte. Do a bitwise or of them, and you end up with +-+ Which combines all the integers you created above, each with only 8 bits remaining from the original x. The 2 leftmost bytes are handled similarly, just x is shifted left to accomplish the same thing.įinally you have newbyte0 | newbyte1 | newbyte2 | newbyte3 The result looks like this with, with only Byte 3 remaining of the original value of x +-+ Shifts the bits 8 bits to the right, and masks off everything but the 8 bits in the 2. Similarely int newbyte1 = (x > 8) & 0xff00 This takes the integer (the 4 bytes) depicted above, shifts it 24 bits to the right, and masks off everything but the lower 8 bits, newbyte0 looks like this now, where Byte 4 is the original Byte 4 of x and the other 3 bytes have all bits set to zero. So your functions convert an integer from one endian to another. Other CPUs will do the reverse, they will treat the rightmost byte within an integer, the byte with the largest memory address as the most significant byte - little endian CPUs. When interpreting an integer that is made up of several bytes, some CPU families will treat the leftmost byte, the one with a lower memory address as the most significant byte of the integer - such CPUs are called big endian. This could relate to the Endianess of a given CPU type. Let's say these 4 bytes are laid out in memory like so: +-+ The first approach is simplest and most widely used, though it does result in 2 unnecessary endian-ness conversions if both the sender and receiver are little-endian.The code assumes a 32 bit integer and 8 bit bytes. via a flag in the packet) and decodes accordingly. The receiver figures out what order the sender used (e.g. The sender finds out what order the receiver expects and uses that order when assembling the data. Non-java applications on little-endian machines need to flip the bytes. Some alternative strategies for knowing / figuring this out are:Įveryone uses "network order" (big-endian) for stuff on the wire as per the example code above. However, if you are sending or receiving packets to some remote application that is implemented in (say) C or C++, you need to "know" what byte order is being used in the network packets. So for Java to Java communication, this should not be an issue. and then discover that different architectures store the bytes of an integer in different orders. This code is 100% portable, assuming that you use the reverse algorithm to create the byte array in the first place.īyte order problems arise in languages where you can cast between a native integer type and byte array type. You need to specify the byte order of the array, but assuming that the bytes is the most significant byte then: int res = ((bytes & 0xff) << 24) | ((bytes & 0xff) << 16) |
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |