简体   繁体   English

从int64转换为字节数组

[英]Cast from int64 to byte array

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 ? 现在的问题是将int64直接转换为字节是否安全? 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. 我很清楚执行此操作的其他安全方法(例如Bitshifting),但我希望代码尽可能快。

Thanks ! 谢谢 !

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. 这就是为什么位移会是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM