简体   繁体   English

C ++ DWORD *到BYTE *

[英]C++ DWORD* to BYTE*

My issue, i am trying to convert and array of dynamic memory of type DWORD to a BYTE. 我的问题,我正在尝试将DWORD类型的动态内存和数组转换为BYTE。 Fair enough i can for loop through this and convert the DWORD into a BYTE per entry. 足够公平,我可以循环浏览并将每个条目的DWORD转换为BYTE。

But is their a faster way to do this? 但是他们是这样做的更快方法吗? to take a pointer to DWORD data and convert the whole piece of data into a pointer to BYTE data? 拿一个指向DWORD数据的指针并将整个数据转换成一个指向BYTE数据的指针? such as using a memcpy operation? 例如使用memcpy操作?

I feel this is not possible, im not requesting an answer just an experienced opinion on my approach, as i have tried testing both approaches but seem to fail getting to a solution on my second solution. 我觉得这是不可能的,因为我尝试测试这两种方法,但似乎无法在我的第二个解决方案上找到解决方案,因此我不希望仅对我的方法提供经验丰富的意见。

Thanks for any input, again no answers just a point in the right direction. 感谢您的任何输入,再次没有答案只是朝着正确的方向。 Nor is this a homework question, i felt that had to be mentioned. 我认为这也不是一个家庭作业问题,必须提到。

It really depends on what you're trying to achieve in the end. 这实际上取决于最终要实现的目标。 If you've got, eg: 如果有,例如:

DWORD *x;
x = new DWORD[100];

// x gets filled in somehow

And you just want to deal with each byte of each DWORD individually then doing something like you suggest (ie using eg memcpy) could be an option. 而且,您只想单独处理每个DWORD的每个字节,然后执行您建议的操作(例如,使用memcpy)即可。 You could, however, just deal with the array as a BYTE array usinga reinterpret_cast eg 但是,您可以使用reinterpret_cast将数组作为BYTE数组处理,例如

BYTE *y = reinterpret_cast<BYTE *>(x);

Or something like that. 或类似的东西。

you can just do static_cast<BYTE*>(array_ptr) if you don't care about endianness possibly swapping the byte order (if you are on little endian), if you do care about endianness, you'll need to run thought with a byte swap before hand (there is an x86 intrinsic for byte swapping). 如果您不关心字节序,您可以只进行static_cast<BYTE*>(array_ptr)交换字节顺序(如果您使用小字节序),如果您关心字节序,则需要使用事先进行字节交换(字节交换有一个x86内在函数 )。

If you need a copy, then just allocate enough space and do a memcpy or a copy loop with the endian fixup. 如果您需要一个副本,则只需分配足够的空间,然后使用endian fixup进行memcpy或副本循环。

DWORD array[10];
BYTE* byteArray = array;
byteArray[0] ....

like this? 像这样?

If DWORD and BYTE are the same length, then yes. 如果DWORD和BYTE的长度相同,则为是。 Otherwise, no, you will need to individually resize each element. 否则,您将需要单独调整每个元素的大小。

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

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