判断程序运行的平台是little-endian还是big-endian
#include <stdio.h>
//判断程序运行的平台是little-endian还是big-endian
bool IsLittleEndian()
{
int i = 1;
char* p = (char*)&i;
return *p;
}
int main()
{
if(IsLittleEndian())
printf("LittleEndian\n");
else
printf("BigEndian\n");
return 0;
}
big-endian The most significant byte is on the left end of a word.
little-endian The most significant byte is on the right end of a word.
example:
big: [1000]4F,[1001]52 表示4f52
little: [1000]52,[1001]4f 表示4f52
3. Converting Small endian to Big Endian using C#(long value)
Could someone tell me how to write an equivalent code in C# for the following C++ code which would give me a big endian from a small endian? Thanking you in Advance..
Function call - ProcessEndian((char*) &longValue,
sizeof(long));
void CUtility::ProcessEndian(char * pHostData, int nHostDataLength)
{
char buffer[10];
int i = 0;
char* pChar = NULL;
for (i=0; i<nHostDataLength; i++)
{
pChar = pHostData+i;
buffer[ i ] = *(pChar);
}
for (i=0; i<nHostDataLength; i++)
{
*pChar = buffer[ i ];
pChar--;
}
}
Function call - ProcessEndian((char*) &longValue,
sizeof(long));
void CUtility::ProcessEndian(char * pHostData, int nHostDataLength)
{
char buffer[10];
int i = 0;
char* pChar = NULL;
for (i=0; i<nHostDataLength; i++)
{
pChar = pHostData+i;
buffer[ i ] = *(pChar);
}
for (i=0; i<nHostDataLength; i++)
{
*pChar = buffer[ i ];
pChar--;
}
}