Question

I wrote the following code which cast a long long to a byte array.

BYTE Buffer[8 +32];
BYTE * Temp = reinterpret_cast<BYTE*> (&Size);
Buffer[0] = Temp[0]; Buffer[1] = Temp[1]; Buffer[2] = Temp[2]; Buffer[3] = Temp[3]; 
Buffer[4] = Temp[4]; Buffer[5] = Temp[5]; Buffer[6] = Temp[6]; Buffer[7] = Temp[7];
//The next 32 bytes (Buffer[8] to Buffer[39]) contains the file name.
WriteFile(hFile,Buffer,40,&dwWrite,NULL);

Now the question is Is it safe to cast an int64 directly into bytes ? What are the possible bugs ?

I am well aware of other safer methods to do this (Bitshifting for example) but i want the code to be as fast as possible.

Thanks !

Was it helpful?

Solution

Problem is you write to disk with the byte ordering of the current machine. If you read it again on a machine with different byte ordering you will run into big trouble.

Thats why bit shifting would be the better way to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top