4
* 2001-February-3 Jason Rohrer
6
* Fixed a bug in the big-endian code.
7
* Fixed parameter names to match convention.
9
* 2001-February-26 Jason Rohrer
10
* Fixed a bug in the little-endian implementation.
12
* 2001-August-28 Jason Rohrer
13
* Changed to be FreeBSD compatible.
15
* 2002-March-13 Jason Rohrer
16
* Started to change to work with solaris.
17
* Finished changing to work with solaris.
19
* 2002-April-11 Jason Rohrer
20
* Added a default BSD case to work with OSX.
22
* 2002-May-25 Jason Rohrer
23
* Changed to use minorGems endian.h
27
#include "minorGems/io/TypeIO.h"
28
#include "minorGems/system/endian.h"
34
* Linux-specific type input and output.
35
* Note that all types are output in the order that
36
* a big-endian linux machine outputs them with no conversion.
41
#if __BYTE_ORDER == __LITTLE_ENDIAN
44
void TypeIO::doubleToBytes( double inDouble, unsigned char *outBytes ) {
46
unsigned char *doubleBuffer = (unsigned char*)( &inDouble );
48
// output second word first
49
outBytes[0] = doubleBuffer[7];
50
outBytes[1] = doubleBuffer[6];
51
outBytes[2] = doubleBuffer[5];
52
outBytes[3] = doubleBuffer[4];
54
outBytes[4] = doubleBuffer[3];
55
outBytes[5] = doubleBuffer[2];
56
outBytes[6] = doubleBuffer[1];
57
outBytes[7] = doubleBuffer[0];
62
double TypeIO::bytesToDouble( unsigned char *inBytes ) {
66
unsigned char *doubleBuffer = (unsigned char*)( &returnValue );
68
// put first word at the end of this double
69
doubleBuffer[7] = inBytes[0];
70
doubleBuffer[6] = inBytes[1];
71
doubleBuffer[5] = inBytes[2];
72
doubleBuffer[4] = inBytes[3];
74
doubleBuffer[3] = inBytes[4];
75
doubleBuffer[2] = inBytes[5];
76
doubleBuffer[1] = inBytes[6];
77
doubleBuffer[0] = inBytes[7];
88
#if __BYTE_ORDER == __BIG_ENDIAN
92
void TypeIO::doubleToBytes( double inDouble, unsigned char *outBytes ) {
94
unsigned char *doubleBuffer = (unsigned char*)( &inDouble );
96
// output in stored order
97
outBytes[0] = doubleBuffer[0];
98
outBytes[1] = doubleBuffer[1];
99
outBytes[2] = doubleBuffer[2];
100
outBytes[3] = doubleBuffer[3];
102
outBytes[4] = doubleBuffer[4];
103
outBytes[5] = doubleBuffer[5];
104
outBytes[6] = doubleBuffer[6];
105
outBytes[7] = doubleBuffer[7];
110
double TypeIO::bytesToDouble( unsigned char *inBytes ) {
114
unsigned char *doubleBuffer = (unsigned char*)( &returnValue );
116
// store in input order
117
doubleBuffer[0] = inBytes[0];
118
doubleBuffer[1] = inBytes[1];
119
doubleBuffer[2] = inBytes[2];
120
doubleBuffer[3] = inBytes[3];
122
doubleBuffer[4] = inBytes[4];
123
doubleBuffer[5] = inBytes[5];
124
doubleBuffer[6] = inBytes[6];
125
doubleBuffer[7] = inBytes[7];