endianness

C Macro definition to determine big endian or little endian machine?

独自空忆成欢 提交于 2019-11-26 01:41:14
问题 Is there a one line macro definition to determine the endianness of the machine. I am using the following code but converting it to macro would be too long. unsigned char test_endian( void ) { int test_var = 1; unsigned char test_endian* = (unsigned char*)&test_var; return (test_endian[0] == NULL); } 回答1: Code supporting arbitrary byte orders, ready to be put into a file called order32.h : #ifndef ORDER32_H #define ORDER32_H #include <limits.h> #include <stdint.h> #if CHAR_BIT != 8 #error

Why does optimisation kill this function?

纵然是瞬间 提交于 2019-11-26 01:15:48
问题 We recently had a lecture in university about programming specials in several languages. The lecturer wrote down the following function: inline u64 Swap_64(u64 x) { u64 tmp; (*(u32*)&tmp) = Swap_32(*(((u32*)&x)+1)); (*(((u32*)&tmp)+1)) = Swap_32(*(u32*) &x); return tmp; } While I totally understand that this is also really bad style in terms of readability, his main point was that this part of code worked fine in production code until they enabled a high optimization level. Then, the code

convert big endian to little endian in C [without using provided func] [closed]

我们两清 提交于 2019-11-25 23:34:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I need to write a function to convert big endian to little endian in C. I can not use any library function. 回答1: Assuming what you need is a simple byte swap, try something like Unsigned 16 bit conversion: swapped = (num>>8) | (num<<8); Unsigned 32-bit conversion: swapped = ((num>>24)&0xff) | // move byte 3 to

Detecting endianness programmatically in a C++ program

爱⌒轻易说出口 提交于 2019-11-25 22:34:00
问题 Is there a programmatic way to detect whether or not you are on a big-endian or little-endian architecture? I need to be able to write code that will execute on an Intel or PPC system and use exactly the same code (i.e. no conditional compilation). 回答1: I don't like the method based on type punning - it will often be warned against by compiler. That's exactly what unions are for ! bool is_big_endian(void) { union { uint32_t i; char c[4]; } bint = {0x01020304}; return bint.c[0] == 1; } The

How do I convert between big-endian and little-endian values in C++?

核能气质少年 提交于 2019-11-25 22:22:56
问题 How do I convert between big-endian and little-endian values in C++? EDIT: For clarity, I have to translate binary data (double-precision floating point values and 32-bit and 64-bit integers) from one CPU architecture to another. This doesn\'t involve networking, so ntoh() and similar functions won\'t work here. EDIT #2: The answer I accepted applies directly to compilers I\'m targetting (which is why I chose it). However, there are other very good, more portable answers here. 回答1: If you're

Convert a byte array to integer in Java and vice versa

ぐ巨炮叔叔 提交于 2019-11-25 20:54:32
I want to store some data into byte arrays in Java. Basically just numbers which can take up to 2 Bytes per number. I'd like to know how I can convert an integer into a 2 byte long byte array and vice versa. I found a lot of solutions googling but most of them don't explain what happens in the code. There's a lot of shifting stuff I don't really understand so I would appreciate a basic explanation. Use the classes found in the java.nio namespace, in particular, the ByteBuffer . It can do all the work for you. byte[] arr = { 0x00, 0x01 }; ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian

C Macro definition to determine big endian or little endian machine?

ε祈祈猫儿з 提交于 2019-11-25 20:17:50
Is there a one line macro definition to determine the endianness of the machine. I am using the following code but converting it to macro would be too long. unsigned char test_endian( void ) { int test_var = 1; unsigned char test_endian* = (unsigned char*)&test_var; return (test_endian[0] == NULL); } Christoph Code supporting arbitrary byte orders, ready to be put into a file called order32.h : #ifndef ORDER32_H #define ORDER32_H #include <limits.h> #include <stdint.h> #if CHAR_BIT != 8 #error "unsupported char size" #endif enum { O32_LITTLE_ENDIAN = 0x03020100ul, O32_BIG_ENDIAN = 0x00010203ul