-
Notifications
You must be signed in to change notification settings - Fork 1
/
encodings.h
161 lines (133 loc) · 3.84 KB
/
encodings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*************************************************
* Publicly released by Rhoban System, August 2012
* www.rhoban-system.fr
*
* Freely usable for non-commercial purposes
*
* Licence Creative Commons *CC BY-NC-SA
* http://creativecommons.org/licenses/by-nc-sa/3.0
*************************************************/
/*
* encodings.h
*
* Created on: 21 mars 2011
* Author: hugo
*/
#ifndef ENCODINGS_H_
#define ENCODINGS_H_
#include <iostream>
#include "types.h"
using namespace std;
/*!
* encode and decode values to and from a buffer
*/
inline void encode_int(int value, char * buf);
inline void encode_uint(ui32 value, char * buf);
inline void encode_short(short value, char * buf);
inline void encode_ushort(unsigned short value, char * buf);
//static float double_coef = 1000.0;
inline void encode_float(float value, char * buf);
/*!
* todo: find a better encoding,
* this one wont be properly decoded on every architecture
*/
inline ui32 decode_uint(const char * buf);
inline int decode_int(const char * buf);
inline unsigned short decode_ushort(const char * buf);
inline short decode_short(const char * buf);
inline float decode_float(const char * buf);
inline string decode_string(const char * buf);
/*!
* encode and decode values to and from a buffer
*/
inline void encode_int(int value, char * buf)
{
//faster solution but there may be big/small endian problems
//as well as alignment problems
//((int *) buf)[0] = value;
buf[0] = (char)((value & 0xff000000) >> 24);
buf[1] = (char)((value & 0x00ff0000) >> 16);
buf[2] = (char)((value & 0x0000ff00) >> 8);
buf[3] = (char)((value & 0x000000ff) >> 0);
}
inline void encode_uint(ui32 value, char * buf)
{
//faster solution but there may be big/small endian problems
//as well as alignment problems
//((ui32 *) buf)[0] = value;
buf[0] = (char)((value & 0xff000000) >> 24);
buf[1] = (char)((value & 0x00ff0000) >> 16);
buf[2] = (char)((value & 0x0000ff00) >> 8);
buf[3] = (char)((value & 0x000000ff) >> 0);
}
inline void encode_short(short value, char * buf)
{
buf[0] = (char)((value & 0xff00) >> 8);
buf[1] = (char)((value & 0x00ff) >> 0);
}
inline void encode_ushort(unsigned short value, char * buf)
{
buf[0] = (char)((value & 0xff00) >> 8);
buf[1] = (char)((value & 0x00ff) >> 0);
}
//static float double_coef = 1000.0;
inline void encode_float(float value, char * buf)
{
encode_uint(*(int *)&value, buf);
// encode_int((int) (value * 1000), buf);
}
inline void encode_double(double value, char * buf)
{
encode_float((float) value, buf);
}
/*!
* todo: find a better encoding,
* this one wont be properly decoded on every architecture
*/
inline ui32 decode_uint(const char * buf)
{
//return ((ui32 *) buf)[0];
//cout << "Decoding int \t"<<((ui32 *) buf)[0]<< "\t\t("<<(int) buf[0]<<" "<< (int) buf[1]<<" "<< (int) buf[2]<<" "<< (int) buf[3]<<")"<<endl;
ui8* buff = (ui8*)buf;
ui32 a = buff[0] << 24;
ui32 b = buff[1] << 16;
ui32 c = buff[2] << 8;
ui32 d = buff[3];
return a | b | c | d;
// return ((ui32)buf[0])<<24 | ((ui32)buf[1])<<16 | ((ui32)buf[2])<<8 | ((ui32)buf[3]);
}
inline int decode_int(const char * buf)
{
ui8* buff = (ui8*)buf;
ui32 a = buff[0] << 24;
ui32 b = buff[1] << 16;
ui32 c = buff[2] << 8;
ui32 d = buff[3];
return (int)(a | b | c | d);
}
inline unsigned short decode_ushort(const char * buf)
{
ui8* buff = (ui8*)buf;
unsigned short a = buff[0] << 8;
unsigned short b = buff[1] << 0;
return a | b;
}
inline short decode_short(const char * buf)
{
ui8* buff = (ui8*)buf;
unsigned short a = buff[0] << 8;
unsigned short b = buff[1] << 0;
return (short)(a | b);
}
inline float decode_float(const char * buf)
{
//return ldexp( decode_uint(buf+1) ,(int) buf[0]-128);
int val = decode_uint(buf);
return *(float *)&val;
}
inline string decode_string(const char * buf)
{
ui32 length = decode_uint(buf);
return string(buf + 4, length);
}
#endif /* ENCODINGS_H_ */