• Advertisement

Archived

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

Quick Question: How to transform bytes in integers?

This topic is 5692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Use the structures provided in the windows headers, look up BITMAPFILEHEADER, BITMAPINFO and BITMAPINFOHEADER in MSDN. These will make your life much easier.


codeka.com - Just click it.

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
Actually, I have no idea of what is happening with this piece of code, and I used the C style. Pointers and stuff are something that i just don''t get.

Anyway, thank you all for the help.

Share this post


Link to post
Share on other sites

  • Advertisement