Jump to content
  • Advertisement
Sign in to follow this  
dsage

bitshift?

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

I have 4 bytes I recieved from a socket. I need to turn the 4 bytes into an unsigned int. Can i do this with bitshift? and how? Thanks...

Share this post


Link to post
Share on other sites
Advertisement
unsigned int data = (unsigned int)byte1 | ((unsigned int)byte2 << 8) | ((unsigned int)byte3 << 16) | ((unsigned int)byte4 << 24);

Note that this assumes little-endianness, which may or may not be correct for your situation.

Share this post


Link to post
Share on other sites
Quote:
Original post by dsage
I have 4 bytes I recieved from a socket. I need to turn the 4 bytes into an unsigned int. Can i do this with bitshift? and how? Thanks...
Something like the following should work:
unsigned int result =
(unsigned int)byte[0] << 0 |
(unsigned int)byte[1] << 8 |
(unsigned int)byte[2] << 16 |
(unsigned int)byte[3] << 24;
You may have to reverse the index order depending on endian-ness.

[Edit: beaten.]

Share this post


Link to post
Share on other sites
I'm getting some strange output...

byteCount = recv(client, buffer, 4, 0);
std::cout << "bytes recv: " << byteCount << std::endl;
std::cout << "buffer: " << buffer << std::endl;
number = ((unsigned int)buffer[0] << 0) | ((unsigned int)buffer[1] << 8) | ((unsigned int)buffer[2] << 16) | ((unsigned int)buffer[3] << 24);
std::cout << "number: " << number << std::endl;[\source]

output:
bytes recv: 4
buffer: ±äòm
number: 4294967281
bytes recv: 4
buffer: j.&#8319;,
number: 4294717034
bytes recv: 4
buffer: &#9580;F&#8730;H
number: 4294967246
bytes recv: 4
buffer: 2&#8593;ªA
number: 4289075250

Share this post


Link to post
Share on other sites
unsigned int n = * (unsigned int *) buffer;

This assumes "buffer" is an array of bytes. Please ensure that you actually have sizeof(unsigned int) bytes before doing this (or any of the other solutions).

Depending on the protocol that you're speaking you may also need the following to get the bytes in the right order:

n = ntohl(n)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
unsigned int n = * (unsigned int *) buffer;


However, this is not possible on CPUs where unaligned access causes a crash.

Share this post


Link to post
Share on other sites
It seems your input data is big-endian. Try switching the order in which the bytes are processed:


number = ((unsigned int)buffer[3] << 0) | ((unsigned int)buffer[2] << 8) | ((unsigned int)buffer[1] << 16) | ((unsigned int)buffer[0] << 24);

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!