Network byte order conversion with "char"
Solution 1
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.
Solution 2
Exactly how many ways can you order the bytes in a single char?
Solution 3
Your networking stack will handle the bits inside the bytes correctly, you must only concern yourself with getting the bytes in the right order.
Solution 4
You need to consider what each function does. From that, you need to apply that knowledge to the size of the type you intend to modify. Consider the following:
#include <stdio.h>
#include <netinet/in.h>
int main () {
uint16_t i = 42;
uint8_t c = 42; // a char
printf ("(uint16_t ) %08X (%d)\n", i, i);
printf ("( htons ) %08X (%d)\n", htons(i), htons(i));
printf ("( uint8_t ) %08X (%c)\n", c, c);
printf ("( htons ) %08X (%c)\n", htons(c), htons(c));
return 0;
}
(uint16_t ) 0000002A (42)
( htons ) 00002A00 (10752)
( uint8_t ) 0000002A (*)
( htons ) 00002A00 ()
Solution 5
You don't read individual bits off the wire, just bytes. Regardless of the endianness, a single byte is the same backwards and forwards just like the word "I" is the same backwards and forwards.
Comments
-
Rev316 almost 2 years
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.