endianness

Converting 32-bit unsigned integer (big endian) to long and back

余生长醉 提交于 2019-11-27 16:33:35
问题 I have a byte[4] which contains a 32-bit unsigned integer (in big endian order) and I need to convert it to long (as int can't hold an unsigned number). Also, how do I do it vice-versa (i.e. from long that contains a 32-bit unsigned integer to byte[4])? 回答1: Sounds like a work for the ByteBuffer. Somewhat like public static void main(String[] args) { byte[] payload = toArray(-1991249); int number = fromArray(payload); System.out.println(number); } public static int fromArray(byte[] payload){

Converting a UINT32 value into a UINT8 array[4]

不打扰是莪最后的温柔 提交于 2019-11-27 16:08:09
问题 My question is how do you convert a UINT32 value to a UINT8 array[4] (C/C++) preferably in a manner independent of endianness? Additionally, how would you reconstruct the UINT32 value from the UINT8 array[4], to get back to where you started? 回答1: You haven't really said what you mean by independent of endianness - it's unclear since the byte array must have some endianness. That said, one of the below must answer your requirements: Given UINT32 v and UINT8 a[4] : "Host" endian (use the

Fast reading of little endian integers from file

微笑、不失礼 提交于 2019-11-27 15:17:05
问题 I need to read a binary file consisting of 4 byte integers (little endian) into a 2D array for my Android application. My current solution is the following: DataInputStream inp = null; try { inp = new DataInputStream(new BufferedInputStream(new FileInputStream(procData), 32768)); } catch (FileNotFoundException e) { Log.e(TAG, "File not found"); } int[][] test_data = new int[SIZE_X][SIZE_Y]; byte[] buffer = new byte[4]; ByteBuffer byteBuffer = ByteBuffer.allocate(4); for (int i=0; i < SIZE_Y;

Is using an union in place of a cast well defined?

泄露秘密 提交于 2019-11-27 15:04:14
I had a discussion this morning with a colleague regarding the correctness of a "coding trick" to detect endianness. The trick was: bool is_big_endian() { union { int i; char c[sizeof(int)]; } foo; foo.i = 1; return (foo.c[0] == 1); } To me, it seems that this usage of an union is incorrect because setting one member of the union and reading another is not well-defined. But I have to admit that this is just a feeling and I lack actual proofs to strengthen my point. Is this trick correct ? Who is right here ? Your code is not portable. It might work on some compilers or it might not. You are

How do I convert a value from host byte order to little endian?

人走茶凉 提交于 2019-11-27 14:47:31
I need to convert a short value from the host byte order to little endian. If the target was big endian, I could use the htons() function, but alas - it's not. I guess I could do: swap(htons(val)) But this could potentially cause the bytes to be swapped twice, rendering the result correct but giving me a performance penalty which is not alright in my case. Something like the following: unsigned short swaps( unsigned short val) { return ((val & 0xff) << 8) | ((val & 0xff00) >> 8); } /* host to little endian */ #define PLATFORM_IS_BIG_ENDIAN 1 #if PLATFORM_IS_LITTLE_ENDIAN unsigned short htoles(

Same output for htonl() and ntohl() on an integer

别来无恙 提交于 2019-11-27 14:44:11
I ran the following program on little-endian [LE] machine [Linux, Intel processor]. I am unable to explain the 3 outputs in below code snippet. Since machine is LE, the value of a is stored as 0x78563412 . When printing, it is displaying its actual value. Since its an LE machine, I expect ntohl() to be a no-op and display 0x78563412 , which it is doing. However, I expect 0x12345678 for 2nd print statement containing htonl() . Can someone please help me understand why they are same? int main() { int a = 0x12345678; printf("Original - 0x%x\n", (a)); printf("Network - 0x%x\n", htonl(a)); printf(

How do I handle byte order differences when reading/writing floating-point types in C?

瘦欲@ 提交于 2019-11-27 14:28:06
I'm devising a file format for my application, and I'd obviously like for it to work on both big-endian and little-endian systems. I've already found working solutions for managing integral types using htonl and ntohl , but I'm a bit stuck when trying to do the same with float and double values. Given the nature of how floating-point representations work, I would assume that the standard byte-order functions won't work on these values. Likewise, I'm not even entirely sure if endianness in the traditional sense is what governs the byte order of these types. All I need is consistency. A way to

Reversing byte order in .NET

谁说我不能喝 提交于 2019-11-27 14:24:05
In the code below, why do X and Y take on different values than what I would think intuitively? If the bytes 0-7 are written to the buffer, shouldn't the resulting long have bytes in the same order? It's like it's reading the long values in reverse order. x 0x0706050403020100 long y 0x0706050403020100 long z 0x0001020304050607 long MemoryStream ms = new MemoryStream(); byte[] buffer = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; ms.Write(buffer, 0, buffer.Length); ms.Flush(); ms.Position = 0; BinaryReader reader = new BinaryReader(ms); long x = reader.ReadInt64(); long y =

Convert “little endian” hex string to IP address in Python

倖福魔咒の 提交于 2019-11-27 12:33:37
问题 What's the best way to turn a string in this form into an IP address: "0200A8C0" . The "octets" present in the string are in reverse order, i.e. the given example string should generate 192.168.0.2 . 回答1: Network address manipulation is provided by the socket module. socket.inet_ntoa(packed_ip) Convert a 32-bit packed IPv4 address (a string four characters in length) to its standard dotted-quad string representation (for example, ‘123.45.67.89’). This is useful when conversing with a program

Marshal.PtrToStructure (and back again) and generic solution for endianness swapping

懵懂的女人 提交于 2019-11-27 11:36:37
I have a system where a remote agent sends serialized structures (from an embedded C system) for me to read and store via IP/UDP. In some cases I need to send back the same structure types. I thought I had a nice setup using Marshal.PtrToStructure (receive) and Marshal.StructureToPtr (send). However, a small gotcha is that the network big endian integers need to be converted to my x86 little endian format to be used locally. When I'm sending them off again, big endian is the way to go. Here are the functions in question: private static T BytesToStruct<T>(ref byte[] rawData) where T: struct { T