10 #include "byteorder.h"
14 SwapBytes2 (void* buffer)
16 unsigned char* p = static_cast<unsigned char*>(buffer);
17 unsigned char temp = p[0];
22 // 0<->3 1<->2 = 0123 -> 3210
24 SwapBytes4 (void* buffer)
26 unsigned char* p = static_cast<unsigned char*>(buffer);
27 unsigned char temp = p[0];
35 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
37 SwapBytes8 (void* buffer)
39 unsigned char* p = static_cast<unsigned char*>(buffer);
40 unsigned char temp = p[0];
55 ConvertNetworkOrder (void* buffer, size_t bytes)
57 #if ! defined (WORDS_BIGENDIAN)
61 char* start = static_cast<char*>(buffer);
62 char* end = start + bytes - 1; // last byte
63 size_t nSwap = bytes / 2;
66 unsigned char c = *start;
74 ConvertReverseNetworkOrder (void* buffer, size_t bytes)
76 #if defined (WORDS_BIGENDIAN)
80 char* start = static_cast<char*>(buffer);
81 char* end = start + bytes - 1; // last byte
82 size_t nSwap = bytes / 2;
85 unsigned char c = *start;
93 write_nint16 (kuint16 const *n_in, int fd)
97 #ifndef WORDS_BIGENDIAN
101 if (write (fd, &n, 2) != 2)
108 read_nint16 (kuint16 *n_out, int fd)
110 if (read (fd, n_out, 2) != 2)
113 #ifndef WORDS_BIGENDIAN
121 write_nint32 (kuint32 const *n_in, int fd)
125 #ifndef WORDS_BIGENDIAN
129 if (write (fd, &n, 4) != 4)
136 read_nint32 (kuint32 *n_out, int fd)
138 if (read (fd, n_out, 4) != 4)
141 #ifndef WORDS_BIGENDIAN
149 write_nfloat32 (kfloat32 const *f_in, int fd)
153 #ifndef WORDS_BIGENDIAN
157 if (write (fd, &f, 4) != 4)
164 read_nfloat32 (kfloat32 *f_out, int fd)
166 if (read (fd, f_out, 4) != 4)
169 #ifndef WORDS_BIGENDIAN
177 write_nfloat64 (kfloat64 const *f_in, int fd)
181 #ifndef WORDS_BIGENDIAN
185 if (write (fd, &f, 8) != 8)
192 read_nfloat64 (kfloat64 *f_out, int fd)
194 if (read (fd, f_out, 8) != 8)
197 #ifndef WORDS_BIGENDIAN
206 onetorderstream& onetorderstream::writeInt16 (kuint16 n) {
207 #ifndef WORDS_BIGENDIAN
214 inetorderstream& inetorderstream::readInt16 (kuint16& n) {
216 #ifndef WORDS_BIGENDIAN
222 onetorderstream& onetorderstream::writeInt32 (kuint32 n) {
223 #ifndef WORDS_BIGENDIAN
230 inetorderstream& inetorderstream::readInt32 (kuint32& n) {
232 #ifndef WORDS_BIGENDIAN
238 onetorderstream& onetorderstream::writeFloat32 (kfloat32 n) {
239 #ifndef WORDS_BIGENDIAN
246 inetorderstream& inetorderstream::readFloat32 (kfloat32& n) {
248 #ifndef WORDS_BIGENDIAN
254 onetorderstream& onetorderstream::writeFloat64 (kfloat64 n) {
255 #ifndef WORDS_BIGENDIAN
262 inetorderstream& inetorderstream::readFloat64 (kfloat64& n) {
264 #ifndef WORDS_BIGENDIAN
273 write_rnint16 (kuint16 n, ostream& ostr)
275 #ifdef WORDS_BIGENDIAN
282 read_rnint16 (kuint16& n, istream& istr)
285 #ifdef WORDS_BIGENDIAN
291 write_rnint32 (kuint32 n, ostream& ostr)
293 #ifdef WORDS_BIGENDIAN
300 read_rnint32 (kuint32& n, istream istr)
303 #ifdef WORDS_BIGENDIAN
309 write_rnfloat32 (kfloat32 n, ostream ostr)
311 #ifdef WORDS_BIGENDIAN
318 read_rnfloat32 (kfloat32& n, istream istr)
321 #ifdef WORDS_BIGENDIAN
327 write_rnfloat64 (kfloat64 n, ostream ostr)
329 #ifdef WORDS_BIGENDIAN
336 read_rnfloat64 (kfloat64& n, istream istr)
339 #ifdef WORDS_BIGENDIAN