endianness

What is wrong with this C function to find the endianness of a machine at runtime?

This is what I offered at an interview today. int is_little_endian(void) { union { long l; char c; } u; u.l = 1; return u.c == 1; } My interviewer insisted that c and l are not guaranteed to begin at the same address and therefore, the union should be changed to say char c[sizeof(long)] and the return...

What's the most Pythonic way of determining endianness?

I'm trying to find the best way of working out whether the machine my code is running on is big-endian or little-endian. I have a solution that works (although I haven't tested it on a big-endian machine) but it seems a bit clunky: import struct little_endian = (struct.pack('@h', 1) == struct.pack('<h', 1)) This is just comparing a 'n...

Little/Big Endian

How can I determine if the OS is little/big endian in Java? ...

Fast little-endian to big-endian conversion in ASM

I have an array of uint-types in C#, After checking if the program is working on a little-endian machine, I want to convert the data to a big-endian type. Because the amount of data can become very large but is always even, I was thinking to consider two uint types as an ulong type, for a better performance and program it in ASM, so I am...

Endianness of integers in Python

I'm working on a program where I store some data in an integer and process it bitwise. For example, I might receive the number 48, which I will process bit-by-bit. In general the endianness of integers depends on the machine representation of integers, but does Python do anything to guarantee that the ints will always be little-endian?...

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { unsigned int version : 3; unsigned int type : 1; unsigned int id : 5; unsigned int ...

BinaryWriter Endian issue

I am using BinaryWriter class to write a binary file to disk. When I invoke the Write method, passing an unsigned short value, it writes it in little-endian format. For example: bw.Write(0xA000); writes the value in the binary file as 0x00 0xA0. Is there a way to make BInaryWriter use Big Endian? If not, is it possible to create a new...

ASCII strings and endianness

An intern who works with me showed me an exam he had taken in computer science about endianness issues. There was a question that showed an ASCII string "My-Pizza", and the student had to show how that string would be represented in memory on a little endian computer. Of course, this sounds like a trick question because ASCII strings a...

C++0x constexpr and endianness

A common question that comes up from time to time in the world of C++ programming is compile-time determination of endianness. Usually this is done with barely portable #ifdefs. But does the C++0x constexpr keyword along with template specialization offer us a better solution to this? Would it be legal C++0x to do something like: con...

Fast way to swap bytes in array from big endian to little endian in C#

I'm reading from a binary stream which is big-endian. The BitConverter class does this automatically. Unfortunately, the floating point conversion I need is not the same as BitConverter.ToSingle(byte[]) so I have my own routine from a co-worker. But the input byte[] needs to be in little-endian. Does anyone have a fast way to convert end...

Faster way to swap endianness in C# with 16bit words

There's got to be a faster and better way to swap bytes of 16bit words then this. Does anyone have an idea? public static void Swap(byte[] data) { for (int i = 0; i < data.Length; i += 2) { byte b = data[i]; data[i] = data[i + 1]; data[i + 1] = b; } } ...

C Endian Conversion : bit by bit

I have a special unsigned long (32 bits) and I need to convert the endianness of it bit by bit - my long represents several things all smooshed together into one piece of binary. How do I do it? ...

Python File Slurp w/ endian conversion

It was recently asked how to do a file slurp in python: link text And it was recommended to use something like with open('x.txt') as x: f = x.read() How would I go about doing this to read the file in and convert the endian representation of the data? For example, I have a 1GB binary file that's just a bunch of single precision flo...

Handling Altivec loads and stores regardless of PPC endianness?

I have some SIMD code in Altivec processing 32 bit integer values in parallel. In some cases I want to load the integers as little endian, in other cases as big endian (note: this choice is regardless of the native CPU endianess; it is based on what algorithm is running). Doing the actual byte swap is very easy using Altivec's permute op...

How to convert floating-point to unsigned variable ?

Hi everyone, Could someone please help me with byte ordering regarding floating point variables? Actually the code is working correctly on Solaris, but not on Windows Xp. Here is a piece example of my code: .... int x_snd=1; float y_snd=1.13; struct { int xx_snd; float yy_snd; } data_snd; int x_rec; float y_rec; ...

What are the possible ways to exchange data in binary format between windows and solaris?

Hi Everyone, Could someone please help and tell me if there is any possible way to pass a data structure (i.e. binary format) through internet sockets between a program running on Windows and other program running on Unix? Any idea or link to materials that deal with it would be very appreciated. Thanking you in advance for your help, ...

Endianness in Unix hexdump

The following *nix command pipes a hex representation of an IP and port (127.0.0.1:80) into the hexdump command. printf "\x7F\x00\x00\x01\x00\x50" | hexdump -e '3/1 "%u." /1 "%u:" 1/2 "%u" "\n"' The -e flag allows an arbitrary format to parse the input. In this case, we are parsing the first three octets of the IP into unsigned decima...

How is each byte in an integer stored in CPU / memory?

hi guys, i have tried this char c[4]; int i=89; memcpy(&c[0],&i,4); cout<<(int)c[0]<<endl; cout<<(int)c[1]<<endl; cout<<(int)c[2]<<endl; cout<<(int)c[3]<<endl; the output is like: 89 0 0 0 which pretty trains my stomache cuz i thought the number would be saved in memory like 0x00000059 so how come c[0] is 89 ? i thought it is suppo...

Endian conversion of signed ints

I am receiving big endian data over UDP and converting it to little endian. The source says the integers are signed but when I swap the bytes of the signed ints (specifically 16-bit) I get unrealistic values. When I swap them as unsigned ints I get what I expect. I suppose the source documentation could be incorrect and is actually sendi...

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

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 ...