本文发表在 rolia.net 枫下论坛Hi, All
I have a question about big endian and small endian. Suppose I want to read a 32 bit unsigned integer from a file. When I read from a file, it is saved as big endian format, i.e.
unsigned char byte[4];
fread(byte, 4, file);
I know that byte[0] is the most significant byte and byte[3] is the least significant byte.
In order to convert this four bytes into a 32 bit integer, I use the the following macro:
#define FOURBYTE_INT32 (a, b, c, d)\
{ \
((a) << 24) | ((b) << 16) | ((c) << 8) | (d) \
}
i.e. U32 value = FOURBYTE_INT32 (byte[0], byte[1], byte[2], byte[3]);
On the other hand, in order to write a 32 bit unsigned integer into a file,
I use the the following macro:
#define INT32_FOURCC (value, a, b, c, d) \
{ \
a = ((value) & 0xFF000000) >> 24; \
b = ((value) & 0x00FF0000) >> 16; \
c = ((value) & 0x0000FF00) >> 8; \
d = ((value) & 0x000000FF); \
}
i.e INT32_FOURCC (value, byte[0], byte[1], byte[2], byte[3]);
fwrite(byte, 4, 1, file);
I know this works on small endian processor and compiler, my question is will it still work on big endian processor and compiler?
Thanks a lot.更多精彩文章及讨论,请光临枫下论坛 rolia.net
I have a question about big endian and small endian. Suppose I want to read a 32 bit unsigned integer from a file. When I read from a file, it is saved as big endian format, i.e.
unsigned char byte[4];
fread(byte, 4, file);
I know that byte[0] is the most significant byte and byte[3] is the least significant byte.
In order to convert this four bytes into a 32 bit integer, I use the the following macro:
#define FOURBYTE_INT32 (a, b, c, d)\
{ \
((a) << 24) | ((b) << 16) | ((c) << 8) | (d) \
}
i.e. U32 value = FOURBYTE_INT32 (byte[0], byte[1], byte[2], byte[3]);
On the other hand, in order to write a 32 bit unsigned integer into a file,
I use the the following macro:
#define INT32_FOURCC (value, a, b, c, d) \
{ \
a = ((value) & 0xFF000000) >> 24; \
b = ((value) & 0x00FF0000) >> 16; \
c = ((value) & 0x0000FF00) >> 8; \
d = ((value) & 0x000000FF); \
}
i.e INT32_FOURCC (value, byte[0], byte[1], byte[2], byte[3]);
fwrite(byte, 4, 1, file);
I know this works on small endian processor and compiler, my question is will it still work on big endian processor and compiler?
Thanks a lot.更多精彩文章及讨论,请光临枫下论坛 rolia.net