0

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.

1
  • Yes, its a tga picture file. Im trying create a point in my program where all images being loaded will be in the same structure. Sep 28, 2012 at 15:29

4 Answers 4

5

It really depends on what you're trying to achieve in the end. If you've got, e.g:

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 (i.e. using e.g. memcpy) could be an option. You could, however, just deal with the array as a BYTE array usinga reinterpret_cast e.g.

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

Or something like that.

2
  • I completely forgot about reinterpret_cast! And i have used this in my math classes. This is what i need to learn in programming but i have a lot to test in order to know when and how to use these. Sep 28, 2012 at 15:59
  • FWIW - you could also use, as @Necrolis mentioned, static_cast for this (at least, it's worth a try). There's the issue of related types to consider though. Neither BYTE nor DWORD are C++ defined types as I understand it so are probably #defines in the system you're using (probably #define BYTE unsigned char, #define DWORD unsigned long or something like that). As they're probably both unsigned static_cast should work. If, however, one was signed and the other wasn't, or they were defined as unrelated classes, you might have problems with static_cast and would need reinterpret_cast.
    – cosimo193
    Oct 1, 2012 at 10:09
1

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.

3
  • @Benj: in that case array[index] & 0xFF will do what he needs, but its not really clear what the OP is trying to achieve...
    – Necrolis
    Sep 28, 2012 at 15:33
  • Again thank you, your answer is what i had in mind but needed some clarity on it. I would vote your solution as well as an answer too but i cant, nevertheless vote up has been given! Sep 28, 2012 at 16:00
  • I suppose as i am doing a drawing operation, a static_cast would be more robust? Seeing as in the point of my program i have already set and prepared the data. Sep 28, 2012 at 16:13
0
DWORD array[10];
BYTE* byteArray = array;
byteArray[0] ....

like this?

3
  • this was one of my original attempts but a DWORD is 4 BYTEs so the pointer wouldnt correctly line up, i think. But i really appriciate your response thank you Sep 28, 2012 at 16:02
  • depends how you want to read the data from the dword array. if you want to convert every dword to a byte, then you have to read from 4 in 4. byteArray[0] is going to be the first byte, byteArray[4] the second and so on (or byteArray[3] and byteArray[7] depending if your pc arhitecture is little endian or big endian). Sep 28, 2012 at 16:04
  • Thank you, thats i another approach that may help me. I always imagined it easier to work from a DWORD rather than a BYTE but both seem just as good. i dont know why your answer was voted down. I ll add one to it now. Sep 28, 2012 at 16:10
-1

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

1
  • dword is 4 bytes, byte is one :) Sep 28, 2012 at 15:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.