8

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Schaki
  • 1,697
  • 1
  • 12
  • 14
  • 5
    Please see: [Endianness](http://en.wikipedia.org/wiki/Endianness) and [Difference between Big Endian and little Endian Byte order](http://stackoverflow.com/questions/701624/difference-between-big-endian-and-little-endian-byte-order) – Ivan Aksamentov - Drop Sep 12 '14 at 07:05

2 Answers2

7

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.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Or `ntohs/htons` if you want to byte swap a short instead of a long (which in this context are assumed to be 2x8 bits and 4x8 bits) – MSalters Sep 12 '14 at 07:15
  • Thanks a bunch! That was the missing link I needed. I was reading some c++ code written in VS (not by me), which I now try to compile with gcc. I then discovered some microsoft specific (I think) macros of style _byteswap_DATATYPE() which I couldn't understand. – Schaki Sep 12 '14 at 07:22
  • Note: byte swap instructions (`bswap reg`) are different from endianness conversions. Indeed, if the host machine uses already the network byte order, no action shall be done. – edmz Sep 12 '14 at 08:06
3

As mentioned in the comments, byteswapping is the process of changing a values endianess from one to another. Lets say you have a value in your memory (left address is lowest):

DE AD BE EF   <- big endian

This valu econsists of 4 bytes - in hexadecimal representation two digits are one byte.

If we now assume that the above value is encoded in big endian, then this would mean that the lowest order byte if the very first byte in memory - here the DE. The Intel x86 processor architecture works with little endian, that means the same value as above would look like this in memory:

FE BE AD DE   <- little endian

These two values represent the same value, but have a different endianess.

maxdev
  • 2,491
  • 1
  • 25
  • 50