umdcoder
umdcoder

Reputation: 147

How do I put a single byte into network byte order?

So I have variables:

uint8_t version = 1;
uint8_t ttl = 1;
uint16_t payload_length = 
uint32_t account_identifier = 24027;
uint32_t source_address = 0;
uint32_t destination_address = 0;
uint16_t checksum = 0;
uint16_t protocol = 1;

I want to make all values in network byte order.

I know to use htons() for the 16-bit values and htonl() for the 32-bit values.

So I would do: uint32_t source_address = htonl(0); and uint16_t checksum = htons(0);

What should I use for the 8-bit values?

Upvotes: 2

Views: 1826

Answers (3)

Paul92
Paul92

Reputation: 9062

8 bit values have just one byte, so it doesen't really matter in which byte ordering they are - they will be the same. So use nothing :).

Upvotes: 1

user207421
user207421

Reputation: 310884

What should I use for the 8-bit values.

Nothing. You can't put one byte into order.

Upvotes: 0

Cornstalks
Cornstalks

Reputation: 38218

What should I use for the 8 bit values?

Nothing. There is no "endianness" for 1-byte (8-bit) values. So you don't have to worry about them (which is why there's not hton function for them).

Upvotes: 5

Related Questions