Jump to content
  • Advertisement
Sign in to follow this  
REspawn

C++ Convert array elemets

This topic is 3515 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

Hi, I have a UINT8 array of fixed size, how can I convert the four elements of the array to an int? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by REspawn
Hi,
I have a UINT8 array of fixed size, how can I convert the four elements of the array to an int?

Thanks


If you have 4 then that would be a long or DWORD since there are only 2 bytes in a int but the answer to your question is simple casting, like so:

int newint = (int)YourUINT8Array;

Share this post


Link to post
Share on other sites
sorry if that was unclear, its an array with 20 UINT8's, how would I go about casting the first 4 to an int, then the next 4 to another and so on.

Share this post


Link to post
Share on other sites
Note that an int is not guaranteed to hold 32 bits; it is guaranteed to hold at least 16. Most compilers have 32-bit ints, but it is not guaranteed by the language.

To do it portably you can just add and shift.

unsigned long result = a[0] << 24 + a[1] << 16 + a[2] << 8 + a[3];


There are many tricks for particular architectures and hardware such as casting the bytes directly into an int, but they rely on either implementation defined behavior or officially undefined behavior, so you should avoid them.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodaKiller

If you have 4 then that would be a long or DWORD since there are only 2 bytes in a int but the answer to your question is simple casting, like so:

int newint = (int)YourUINT8Array;


Int is commonly 32 bits these days. Variations exist depending on language and other factors.

The actual conversion depends on what UINT8 is. For example, under Winapi, BOOL is an int.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodaKiller
the answer to your question is simple casting, like so:

int newint = (int)YourUINT8Array;

Very incorrect. Even if you used (int)YourUINT8Array[0] it is still wrong.

* The UINT8 array may not be properly aligned for an int, which gives undefined behavior.
* A 16-bit int will give incorrect results since you only get two UINT8.
* A 64-bit int will give incorrect results since you get eight UINT8.
* Any size other than 32-bit int will give incorrect results.
* Big endian and little endian machines would give different and incorrect results.
* A byte is not guaranteed to be exactly 8 bits. Many chips use 9, 16, 32, or 36 bit bytes, so a "simple" cast on any one of those would undefined results.


The correct and portable way is to do this through bit shifting into an unsigned long which is guaranteed to hold at least 32 bits, as in my earlier post.

Share this post


Link to post
Share on other sites
is memcpy ok / safe to use in a situation like this?

this works just fine...
int valueTwo = 0;
memcpy_s((void*)&valueTwo, 4, (void*)&rawHeader[4], 4);

Share this post


Link to post
Share on other sites
Quote:
Original post by REspawn
is memcpy ok / safe to use in a situation like this?

this works just fine...
int valueTwo = 0;
memcpy_s((void*)&valueTwo, 4, (void*)&rawHeader[4], 4);

Depends on the byte ordering of your machine.

Your array value {0x0A, 0x0B, 0x0C, 0x0D} could become 0x0A0B0C0D or 0x0D0C0B0A. If the machine has 16-bit accessing it could even become 0x0C0D0A0B.

Perform bit shifts to be safe.

Of course, if you are saying to yourself, "I am writing for 32-bit Windows and I don't care if I write incorrect or even illegal C++ code", then you can ignore the advice and use the simple, fast, and unsafe cast.

Share this post


Link to post
Share on other sites
Quote:
Original post by frob
Depends on the byte ordering of your machine.

Your array value {0x0A, 0x0B, 0x0C, 0x0D} could become 0x0A0B0C0D or 0x0D0C0B0A. If the machine has 16-bit accessing it could even become 0x0C0D0A0B.

Perform bit shifts to be safe.


I tried the bit shifts with the end result always being 0.

unsigned long result = rawHeader[4] << 24 + rawHeader[5] << 16 + rawHeader[6] << 8 + rawHeader[7];

this should now hold the number 3, any ideas what I'm doing wrong?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!