简体   繁体   中英

C++ DWORD* to BYTE*

My issue, i am trying to convert and array of dynamic memory of type DWORD to a BYTE. Fair enough i can for loop through this and convert the DWORD into a BYTE per entry.

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? such as using a memcpy operation?

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. You could, however, just deal with the array as a BYTE array usinga reinterpret_cast eg

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).

If you need a copy, then just allocate enough space and do a memcpy or a copy loop with the endian fixup.

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

like this?

If DWORD and BYTE are the same length, then yes. Otherwise, no, you will need to individually resize each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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