Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does performing a byteswap mean? [duplicate]

Tags:

c++

There is dozens of places discussing how to do different kinds of byteswapping, but I couldn't easily find a place explaining the concept and some typical examples of how the need to byteswap occurs.

So here is the question: what is byteswapping and why/when do I need to do it?

If examples are a good way to explain, I would be happy if they where in standard C++. Book references are appreciated, preferentially to Lippman's or Pratas C++ primers since those are the one I have available.

like image 940
Schaki Avatar asked Sep 12 '14 07:09

Schaki


Video Answer


1 Answers

If I understand your question correctly, you're talking about big endian to little endian conversion and back.

That occurs because some microprocessors use little endian format to refer to memory, while others use big endian format.

The bytestreams on the internet are for example, big endian while your intel CPU works with little endian format.

Hence to translate from network to CPU or CPU to network, we need a conversion mechanism called byteswapping.

OSes provide ntohl() and htonl() functions for doing this.

like image 150
Aniket Inge Avatar answered Oct 18 '22 18:10

Aniket Inge