Advertisement

Quick Question: How to transform bytes in integers?

Started by July 27, 2002 07:25 AM
4 comments, last by algumacoisaqualquer 22 years, 3 months ago
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]
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.
Advertisement
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
Hey, it worked!
That was exactly what I was needing, thanks a Lot!
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.
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.

This topic is closed to new replies.

Advertisement