10 #include "byteorder.h"
13 SwapBytes2 (void* buffer)
15 unsigned char* p = static_cast<unsigned char*>(buffer);
16 unsigned char temp = p[0];
21 // 0<->3 1<->2 = 0123 -> 3210
23 SwapBytes4 (void* buffer)
25 unsigned char* p = static_cast<unsigned char*>(buffer);
26 unsigned char temp = p[0];
34 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
36 SwapBytes8 (void* buffer)
38 unsigned char* p = static_cast<unsigned char*>(buffer);
39 unsigned char temp = p[0];
54 ConvertNetworkOrder (void* buffer, size_t bytes)
56 #if ! defined (WORDS_BIGENDIAN)
60 char* start = static_cast<char*>(buffer);
61 char* end = start + bytes - 1; // last byte
62 size_t nSwap = bytes / 2;
65 unsigned char c = *start;
73 ConvertReverseNetworkOrder (void* buffer, size_t bytes)
75 #if defined (WORDS_BIGENDIAN)
79 char* start = static_cast<char*>(buffer);
80 char* end = start + bytes - 1; // last byte
81 size_t nSwap = bytes / 2;
84 unsigned char c = *start;
92 write_nint16 (kuint16 const *n_in, int fd)
96 #ifndef WORDS_BIGENDIAN
100 if (write (fd, &n, 2) != 2)
107 read_nint16 (kuint16 *n_out, int fd)
109 if (read (fd, n_out, 2) != 2)
112 #ifndef WORDS_BIGENDIAN
120 write_nint32 (kuint32 const *n_in, int fd)
124 #ifndef WORDS_BIGENDIAN
128 if (write (fd, &n, 4) != 4)
135 read_nint32 (kuint32 *n_out, int fd)
137 if (read (fd, n_out, 4) != 4)
140 #ifndef WORDS_BIGENDIAN
148 write_nfloat32 (kfloat32 const *f_in, int fd)
152 #ifndef WORDS_BIGENDIAN
156 if (write (fd, &f, 4) != 4)
163 read_nfloat32 (kfloat32 *f_out, int fd)
165 if (read (fd, f_out, 4) != 4)
168 #ifndef WORDS_BIGENDIAN
176 write_nfloat64 (kfloat64 const *f_in, int fd)
180 #ifndef WORDS_BIGENDIAN
184 if (write (fd, &f, 8) != 8)
191 read_nfloat64 (kfloat64 *f_out, int fd)
193 if (read (fd, f_out, 8) != 8)
196 #ifndef WORDS_BIGENDIAN
205 onetorderstream& onetorderstream::writeInt16 (kuint16 n) {
206 #ifndef WORDS_BIGENDIAN
213 inetorderstream& inetorderstream::readInt16 (kuint16& n) {
215 #ifndef WORDS_BIGENDIAN
221 onetorderstream& onetorderstream::writeInt32 (kuint32 n) {
222 #ifndef WORDS_BIGENDIAN
229 inetorderstream& inetorderstream::readInt32 (kuint32& n) {
231 #ifndef WORDS_BIGENDIAN
237 onetorderstream& onetorderstream::writeFloat32 (kfloat32 n) {
238 #ifndef WORDS_BIGENDIAN
245 inetorderstream& inetorderstream::readFloat32 (kfloat32& n) {
247 #ifndef WORDS_BIGENDIAN
253 onetorderstream& onetorderstream::writeFloat64 (kfloat64 n) {
254 #ifndef WORDS_BIGENDIAN
261 inetorderstream& inetorderstream::readFloat64 (kfloat64& n) {
263 #ifndef WORDS_BIGENDIAN
272 write_rnint16 (kuint16 n, ostream& ostr)
274 #ifdef WORDS_BIGENDIAN
281 read_rnint16 (kuint16& n, istream& istr)
284 #ifdef WORDS_BIGENDIAN
290 write_rnint32 (kuint32 n, ostream& ostr)
292 #ifdef WORDS_BIGENDIAN
299 read_rnint32 (kuint32& n, istream istr)
302 #ifdef WORDS_BIGENDIAN
308 write_rnfloat32 (kfloat32 n, ostream ostr)
310 #ifdef WORDS_BIGENDIAN
317 read_rnfloat32 (kfloat32& n, istream istr)
320 #ifdef WORDS_BIGENDIAN
326 write_rnfloat64 (kfloat64 n, ostream ostr)
328 #ifdef WORDS_BIGENDIAN
335 read_rnfloat64 (kfloat64& n, istream istr)
338 #ifdef WORDS_BIGENDIAN