Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Endians 1

Status
Not open for further replies.

ASingerMustDie

Programmer
Feb 1, 2001
17
GB
Hi everyone,

How may one go about writing a buffer to a file in Big-Endian form rather than Little-Endian?

CString buffer;

buffer = 6;

WriteFile(myHandle, buffer, 2, &bWritten, NULL);

Gives (in hex):
06 00

How do we get:
00 06?

Any help most appreciated,
ASMD
 
////////////////////////////////////////////////////////
// SwapBytes
// static function, reverses bytes of the value
//
//Arguments:
// inout_pvData - ptr to value to reverse
// in_iSize - number bytes of the value
//out_pvOut - ptr to result
// (if NULL the pvData is used)
void CFile::SwapBytes (
void* inout_pvData, int in_iSize, void* out_pvOut)
{
const char* pcSrc = (const char*) inout_pvData;
char* pcDest = new char [in_iSize];
for (int i = 0; i < in_iSize; i++)
pcDest [ i ] = pcSrc [ in_iSize - i - 1];
if (out_pvOut == NULL)
out_pvOut = (void*)inout_pvData;
memcpy (out_pvOut, pcDest, in_iSize);
delete pcDest;
}
swap it first then save it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top