Archived

This topic is now archived and is closed to further replies.

algumacoisaqualquer

Quick Question: How to transform bytes in integers?

Recommended Posts

Hello... Well, I have an array of bytes loaded from a bmp file, and I want to stract the width. However, the width is stored in the 18, 19, 20 and 21 elements of the array. So, if I read the array[18], it will return 0, but the actually width is 256, and it is stored in both array[18] and array[19] (I really don't know how this dword thing works). Now, how can I get this in an integer value? I would apreciate any help you coud give me. Thanks a lot! [edited by - algumacoisaqualquer on July 27, 2002 8:26:10 AM]

Share this post


Link to post
Share on other sites
In general you can something like this:

  
unsigned char bytearray[ARRAY_SIZE];
int someint = *(int*)&bytearray[index_of_first_byte_of_int];
// In your case, with the int stored at bytes 18-21, you can:

int width = *(int*)&array[18];
// Or, in the C++ way:

int width = *static_cast<int*>(&array[18]);


-Neophyte

Share this post


Link to post
Share on other sites
You''d want reinterpret_cast in this case, not static_cast. static_cast is used when you want data conversion to happen (like casting a float to an int, for example). Though in this case, they would probably both produce the same output.


codeka.com - Just click it.

Share this post


Link to post
Share on other sites