views:

465

answers:

5

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.

Thanks!

Ref: http://beej.us/guide/bgnet/output/html/multipage/htonsman.html

+3  A: 

Your networking stack will handle the bits inside the bytes correctly, you must only concern yourself with getting the bytes in the right order.

Segfault
+1  A: 

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.

noah
Many ethernet systems do encode individual bits - see http://en.wikipedia.org/wiki/MLT-3, though the faster ones are nibbles.
Pete Kirkham
+2  A: 

Exactly how many ways can you order the bytes in a single char?

Martin Beckett
+1 for snark. To spell it out: by definition in C and C++ a char is the smallest addressable unit of storage.
Derrick Turk
-1 for snark. You know the answer to a question the OP didn't. Good for you. Don't be a jerk about it.
John Dibling
Lighten up - the OP obviously knows about byte ordering and hton etc - why you don't need this for a byte is just one of those d'oh moments.
Martin Beckett
+6  A: 

What you are looking for is endianness.

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

big-endian

while a little-endian architecture stores them in reverse:

little-endian

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.

Michael Myers
+1, but you might want to reconsider stealing the bandwidth of another site.
Bill
@Bill, Wikipedia's policy on hotlinking is apparently at http://commons.wikimedia.org/wiki/Commons:Reusing_content_outside_Wikimedia#Hotlinking -- but wikimedia.org is blocked here, so I can't read it myself. Would you mind paraphrasing it here? Thanks!
Michael Myers
@mmyers: I learned something new today, thanks! Hotlinking paraphrased: "Go to it, but the images might change. Caveat scriptor." Bummer about the blocking.
Bill
+1  A: 

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 ()
ezpz
Thanks for the sample code ezpz!
Rev316