endianness

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

我们两清 提交于 2019-11-26 16:53:51
问题 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()

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

牧云@^-^@ 提交于 2019-11-26 16:45:24
问题 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

Reversing byte order in .NET

非 Y 不嫁゛ 提交于 2019-11-26 16:44:18
问题 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()

How do I convert an array of floats to a byte[] and back?

て烟熏妆下的殇ゞ 提交于 2019-11-26 15:45:10
问题 I have an array of Floats that need to be converted to a byte array and back to a float[]... can anyone help me do this correctly? I'm working with the bitConverter class and found myself stuck trying to append the results. The reason I'm doing this is so I can save runtime values into a IO Stream. The target storage is Azure Page blobs in case that matters. I don't care about what endian this is stored in, as long as it input matches the output. static byte[] ConvertFloatToByteArray(float[]

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

淺唱寂寞╮ 提交于 2019-11-26 15:38:41
问题 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

Floating point Endianness?

流过昼夜 提交于 2019-11-26 14:18:44
问题 I'm writing a client and a server for a realtime offshore simulator, and, as I have to send a lot of data through a socket, I'm using binary data to maximize the ammount of data I can send. I already know about integers endianness, and how to use htonl and ntohl to circumvent endianness issues, but my application, as almost all simulation software, deals with a lot of floats. My question is: Is there some issue of endianness whean dealing with binary formats of floating point numbers? I know

Bitwise operators and “endianness”

白昼怎懂夜的黑 提交于 2019-11-26 13:00:40
Does endianness matter at all with the bitwise operations? Either logical or shifting? I'm working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I'm getting quite hung up on the endianess. That is, I'm using a little endian machine (like most are), but does this need to be considered or is it a wasted fact? In case it matters, I'm using C. Endianness only matters for layout of data in memory. As soon as data is loaded by the processor to be operated on, endianness is completely irrelevent. Shifts, bitwise operations, and so on perform as you

C# little endian or big endian?

早过忘川 提交于 2019-11-26 12:13:59
In the documentation of hardware that allows us to control it via UDP/IP, I found the following fragment: In this communication protocol, DWORD is a 4 bytes data, WORD is a 2 bytes data, BYTE is a single byte data. The storage format is little endian, namely 4 bytes (32bits) data is stored as: d7-d0, d15-d8, d23-d16, d31-d24; double bytes (16bits) data is stored as: d7-d0 , d15-d8. I am wondering how this translates to C#? Do I have to convert stuff before sending it over? For example, if I want to send over a 32 bit integer, or a 4 character string? C# itself doesn't define the endianness.

How does this program work?

余生长醉 提交于 2019-11-26 12:05:09
问题 #include <stdio.h> int main() { float a = 1234.5f; printf(\"%d\\n\", a); return 0; } It displays a 0 !! How is that possible? What is the reasoning? I have deliberately put a %d in the printf statement to study the behaviour of printf . 回答1: That's because %d expects an int but you've provided a float. Use %e / %f / %g to print the float. On why 0 is printed: The floating point number is converted to double before sending to printf . The number 1234.5 in double representation in little endian

Marshalling a big-endian byte collection into a struct in order to pull out values

孤街醉人 提交于 2019-11-26 11:18:22
问题 There is an insightful question about reading a C/C++ data structure in C# from a byte array, but I cannot get the code to work for my collection of big-endian (network byte order) bytes. (EDIT: Note that my real struct has more than just one field.) Is there a way to marshal the bytes into a big-endian version of the structure and then pull out the values in the endianness of the framework (that of the host, which is usually little-endian)? (Note, reversing the array of bytes will not work -