The Quantum Physicist
The Quantum Physicist

Reputation: 26256

C++: Endianness difference within TCP file transfer

I would like to ask about the problems I'm going to get when I transfer strings from one computer to another with different endianness.

I'm reading the data transferred into a std::deque<char>, and then parse it to std::string.

I can imagine that if my computer is LE (little endian) and the computer on the other hand is BE (big endian), then all I have to do is reverse the order of the bytes in total before queuing them and parsing them.

What about PDP endianness? can it be fixed?

Or is this whole idea of fixing wrong because endianness doesn't matter when I transfer a series of 1-byte chars?

Upvotes: 1

Views: 152

Answers (1)

anthonyvd
anthonyvd

Reputation: 7590

It shouldn't matter for 1 byte chars, unless they are interpreted as wider integers on either side.

In case of wider integers though, you should be converting your values to network order before writing them to the network. The receiver will convert them from network to host order on their side.

You typically use the hton/ntoh family of functions for this.

Upvotes: 1

Related Questions