Network byte order conversion with "char"

c++, network-programming

Solution

What you are looking for is endianness.

A big-endian architecture stores the bytes of a multibyte data type like so:

while a little-endian architecture stores them in reverse:

When data is transferred from one machine to another, the bytes of a single data type must be reordered to correspond with the endianness of the destination machine.

But when a data type only consists of one byte, there is nothing to reorder.

Problem

I've always been taught that if an integer is larger than a char, you must solve the byte ordering problem. Usually, I'll just wrap it in the hton[l|s] and convert it back with ntoh[l|s]. But I'm confused why this doesn't apply to single byte characters. I'm sick of wondering why this is, and would love for a seasoned networks programmer to help me shed some light on why byte orderings only apply for multibyte integers. Ref: https://beej.us/guide/bgnet/html/multi/htonsman.html

Original source