Utilities  1
Collection of utility classes and functions used by my C++ applications.
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Friends Macros
binaryconversion.cpp
Go to the documentation of this file.
1 #include "binaryconversion.h"
2 #include "conversionexception.h"
3 
4 #include <iostream>
5 
6 #if defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)
7 # define BYTE_ORDER_1 ByteOrder::BigEndian
8 # define BYTE_ORDER_2 ByteOrder::LittleEndian
9 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN)
10 # define BYTE_ORDER_2 ByteOrder::BigEndian
11 # define BYTE_ORDER_1 ByteOrder::LittleEndian
12 #elif defined(CONVERSION_UTILITIES_BYTE_ORDER_MIDDLE_ENDIAN)
13 # error "Middle endian byte order is not supported!"
14 #else
15 # error "Byte order not determined!"
16 #endif
17 
18 #if defined(CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_LITTLE_ENDIAN)
19 # define FLOAT_BYTE_ORDER_1 ByteOrder::BigEndian
20 # define FLOAT_BYTE_ORDER_2 ByteOrder::LittleEndian
21 #elif defined(CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_BIG_ENDIAN)
22 # define FLOAT_BYTE_ORDER_2 ByteOrder::BigEndian
23 # define FLOAT_BYTE_ORDER_1 ByteOrder::LittleEndian
24 #elif defined(CONVERSION_UTILITIES_FLOAT_BYTE_ORDER_MIDDLE_ENDIAN)
25 # error "Middle endian byte order is not supported!"
26 #else
27 # error "Byte order not determined!"
28 #endif
29 
41 namespace ConversionUtilities
42 {
43 
47 int16 toInt16(const char *value, int startIndex, ByteOrder byteOrder)
48 {
49  switch(byteOrder) {
50  case BYTE_ORDER_1:
51  return (static_cast<int16>(value[startIndex ]) << 8 & 0xFF00)
52  | (static_cast<int16>(value[startIndex + 1]) & 0x00FF);
53  case BYTE_ORDER_2:
54  return (static_cast<int16>(value[startIndex + 1]) << 8 & 0xFF00)
55  | (static_cast<int16>(value[startIndex ]) & 0x00FF);
56  default:
57  return 0;
58  }
59 }
60 
64 uint16 toUInt16(const char *value, int startIndex, ByteOrder byteOrder)
65 {
66  switch(byteOrder) {
67  case BYTE_ORDER_1:
68  return (static_cast<uint16>(value[startIndex ]) << 8 & 0xFF00)
69  | (static_cast<uint16>(value[startIndex + 1]) & 0x00FF);
70  case BYTE_ORDER_2:
71  return (static_cast<uint16>(value[startIndex + 1]) << 8 & 0xFF00)
72  | (static_cast<uint16>(value[startIndex ]) & 0x00FF);
73  default:
74  return 0;
75  }
76 }
77 
81 int32 toInt32(const char *value, int startIndex, ByteOrder byteOrder)
82 {
83  switch(byteOrder) {
84  case BYTE_ORDER_1:
85  return (static_cast<int32>(value[startIndex ]) << 24 & 0xFF000000)
86  | (static_cast<int32>(value[startIndex + 1]) << 16 & 0x00FF0000)
87  | (static_cast<int32>(value[startIndex + 2]) << 8 & 0x0000FF00)
88  | (static_cast<int32>(value[startIndex + 3]) & 0x000000FF);
89  case BYTE_ORDER_2:
90  return (static_cast<int32>(value[startIndex + 3]) << 24 & 0xFF000000)
91  | (static_cast<int32>(value[startIndex + 2]) << 16 & 0x00FF0000)
92  | (static_cast<int32>(value[startIndex + 1]) << 8 & 0x0000FF00)
93  | (static_cast<int32>(value[startIndex ]) & 0x000000FF);
94  default:
95  return 0;
96  }
97 }
98 
102 uint32 toUInt24(const char *value, int startIndex, ByteOrder byteOrder)
103 {
104  switch(byteOrder) {
105  case BYTE_ORDER_1:
106  return (static_cast<uint32>(value[startIndex ]) << 16 & 0x00FF0000)
107  | (static_cast<uint32>(value[startIndex + 1]) << 8 & 0x0000FF00)
108  | (static_cast<uint32>(value[startIndex + 2]) & 0x000000FF);
109  case BYTE_ORDER_2:
110  return (static_cast<uint32>(value[startIndex + 2]) << 16 & 0x00FF0000)
111  | (static_cast<uint32>(value[startIndex + 1]) << 8 & 0x0000FF00)
112  | (static_cast<uint32>(value[startIndex ]) & 0x000000FF);
113  default:
114  return 0;
115  }
116 }
117 
121 uint32 toUInt32(const char *value, int startIndex, ByteOrder byteOrder)
122 {
123  switch(byteOrder) {
124  case BYTE_ORDER_1:
125  return (static_cast<uint32>(value[startIndex ]) << 24 & 0xFF000000)
126  | (static_cast<uint32>(value[startIndex + 1]) << 16 & 0x00FF0000)
127  | (static_cast<uint32>(value[startIndex + 2]) << 8 & 0x0000FF00)
128  | (static_cast<uint32>(value[startIndex + 3]) & 0x000000FF);
129  case BYTE_ORDER_2:
130  return (static_cast<uint32>(value[startIndex + 3]) << 24 & 0xFF000000)
131  | (static_cast<uint32>(value[startIndex + 2]) << 16 & 0x00FF0000)
132  | (static_cast<uint32>(value[startIndex + 1]) << 8 & 0x0000FF00)
133  | (static_cast<uint32>(value[startIndex ]) & 0x000000FF);
134  default:
135  return 0;
136  }
137 }
138 
142 int64 toInt64(const char *value, int startIndex, ByteOrder byteOrder)
143 {
144  switch(byteOrder) {
145  case BYTE_ORDER_1:
146  return (static_cast<int64>(value[startIndex ]) << 56 & 0xFF00000000000000)
147  | (static_cast<int64>(value[startIndex + 1]) << 48 & 0x00FF000000000000)
148  | (static_cast<int64>(value[startIndex + 2]) << 40 & 0x0000FF0000000000)
149  | (static_cast<int64>(value[startIndex + 3]) << 32 & 0x000000FF00000000)
150  | (static_cast<int64>(value[startIndex + 4]) << 24 & 0x00000000FF000000)
151  | (static_cast<int64>(value[startIndex + 5]) << 16 & 0x0000000000FF0000)
152  | (static_cast<int64>(value[startIndex + 6]) << 8 & 0x000000000000FF00)
153  | (static_cast<int64>(value[startIndex + 7]) & 0x00000000000000FF);
154  case BYTE_ORDER_2:
155  return (static_cast<int64>(value[startIndex + 7]) << 56 & 0xFF00000000000000)
156  | (static_cast<int64>(value[startIndex + 6]) << 48 & 0x00FF000000000000)
157  | (static_cast<int64>(value[startIndex + 5]) << 40 & 0x0000FF0000000000)
158  | (static_cast<int64>(value[startIndex + 4]) << 32 & 0x000000FF00000000)
159  | (static_cast<int64>(value[startIndex + 3]) << 24 & 0x00000000FF000000)
160  | (static_cast<int64>(value[startIndex + 2]) << 16 & 0x0000000000FF0000)
161  | (static_cast<int64>(value[startIndex + 1]) << 8 & 0x000000000000FF00)
162  | (static_cast<int64>(value[startIndex ]) & 0x00000000000000FF);
163  default:
164  return 0;
165  }
166 }
167 
171 uint64 toUInt64(const char *value, int startIndex, ByteOrder byteOrder)
172 {
173  switch(byteOrder) {
174  case BYTE_ORDER_1:
175  return (static_cast<uint64>(value[startIndex ]) << 56 & 0xFF00000000000000)
176  | (static_cast<uint64>(value[startIndex + 1]) << 48 & 0x00FF000000000000)
177  | (static_cast<uint64>(value[startIndex + 2]) << 40 & 0x0000FF0000000000)
178  | (static_cast<uint64>(value[startIndex + 3]) << 32 & 0x000000FF00000000)
179  | (static_cast<uint64>(value[startIndex + 4]) << 24 & 0x00000000FF000000)
180  | (static_cast<uint64>(value[startIndex + 5]) << 16 & 0x0000000000FF0000)
181  | (static_cast<uint64>(value[startIndex + 6]) << 8 & 0x000000000000FF00)
182  | (static_cast<uint64>(value[startIndex + 7]) & 0x00000000000000FF);
183  case BYTE_ORDER_2:
184  return (static_cast<uint64>(value[startIndex + 7]) << 56 & 0xFF00000000000000)
185  | (static_cast<uint64>(value[startIndex + 6]) << 48 & 0x00FF000000000000)
186  | (static_cast<uint64>(value[startIndex + 5]) << 40 & 0x0000FF0000000000)
187  | (static_cast<uint64>(value[startIndex + 4]) << 32 & 0x000000FF00000000)
188  | (static_cast<uint64>(value[startIndex + 3]) << 24 & 0x00000000FF000000)
189  | (static_cast<uint64>(value[startIndex + 2]) << 16 & 0x0000000000FF0000)
190  | (static_cast<uint64>(value[startIndex + 1]) << 8 & 0x000000000000FF00)
191  | (static_cast<uint64>(value[startIndex ]) & 0x00000000000000FF);
192  default:
193  return 0;
194  }
195 }
196 
200 float32 toFloat32(const char *value, int startIndex, ByteOrder byteOrder)
201 {
202  int32 val;
203  char *c;
204  switch(byteOrder) {
205  case BYTE_ORDER_1:
206  val = toInt32(value, startIndex, FLOAT_BYTE_ORDER_1);
207  c = reinterpret_cast<char*>(&val);
208  return *reinterpret_cast<float32*>(c);
209  case BYTE_ORDER_2:
210  val = toInt32(value, startIndex, FLOAT_BYTE_ORDER_2);
211  c = reinterpret_cast<char*>(&val);
212  return *reinterpret_cast<float32*>(c);
213  default:
214  return 0.0;
215  }
216 }
217 
221 float64 toFloat64(const char *value, int startIndex, ByteOrder byteOrder)
222 {
223  int64 val;
224  char *c;
225  switch(byteOrder) {
226  case BYTE_ORDER_1:
227  val = toInt64(value, startIndex, FLOAT_BYTE_ORDER_1);
228  c = reinterpret_cast<char*>(&val);
229  return *reinterpret_cast<float64*>(c);
230  case BYTE_ORDER_2:
231  val = toInt64(value, startIndex, FLOAT_BYTE_ORDER_2);
232  c = reinterpret_cast<char*>(&val);
233  return *reinterpret_cast<float64*>(c);
234  default:
235  return 0.0;
236  }
237 }
238 
247 {
248  return ((normalInt & 0x0000007fu) )
249  | ((normalInt & 0x00003f80u) << 1)
250  | ((normalInt & 0x001fc000u) << 2)
251  | ((normalInt & 0x0fe00000u) << 3);
252 }
253 
261 uint32 toNormalInt(uint32 synchsafeInt)
262 {
263  return ((synchsafeInt & 0x0000007fu) )
264  | ((synchsafeInt & 0x00007f00u) >> 1)
265  | ((synchsafeInt & 0x007f0000u) >> 2)
266  | ((synchsafeInt & 0x7f000000u) >> 3);
267 }
268 
272 void getBytes(int16 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
273 {
274  switch(byteOrder) {
275  case BYTE_ORDER_1:
276  outputbuffer[startIndex ] = static_cast<char>((value >> 8) & 0xFF);
277  outputbuffer[startIndex + 1] = static_cast<char>((value ) & 0xFF);
278  break;
279  case BYTE_ORDER_2:
280  outputbuffer[startIndex + 1] = static_cast<char>((value >> 8) & 0xFF);
281  outputbuffer[startIndex ] = static_cast<char>((value ) & 0xFF);
282  break;
283  }
284 }
285 
289 void getBytes(uint16 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
290 {
291  switch(byteOrder) {
292  case BYTE_ORDER_1:
293  outputbuffer[startIndex ] = static_cast<char>((value >> 8) & 0xFF);
294  outputbuffer[startIndex + 1] = static_cast<char>((value ) & 0xFF);
295  break;
296  case BYTE_ORDER_2:
297  outputbuffer[startIndex + 1] = static_cast<char>((value >> 8) & 0xFF);
298  outputbuffer[startIndex ] = static_cast<char>((value ) & 0xFF);
299  break;
300  }
301 }
302 
306 void getBytes(int32 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
307 {
308  switch(byteOrder) {
309  case BYTE_ORDER_1:
310  outputbuffer[startIndex ] = static_cast<char>((value >> 24) & 0xFF);
311  outputbuffer[startIndex + 1] = static_cast<char>((value >> 16) & 0xFF);
312  outputbuffer[startIndex + 2] = static_cast<char>((value >> 8) & 0xFF);
313  outputbuffer[startIndex + 3] = static_cast<char>((value ) & 0xFF);
314  break;
315  case BYTE_ORDER_2:
316  outputbuffer[startIndex + 3] = static_cast<char>((value >> 24) & 0xFF);
317  outputbuffer[startIndex + 2] = static_cast<char>((value >> 16) & 0xFF);
318  outputbuffer[startIndex + 1] = static_cast<char>((value >> 8) & 0xFF);
319  outputbuffer[startIndex ] = static_cast<char>((value ) & 0xFF);
320  break;
321  }
322 }
323 
327 void getBytes(uint32 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
328 {
329  switch(byteOrder) {
330  case BYTE_ORDER_1:
331  outputbuffer[startIndex ] = static_cast<char>((value >> 24) & 0xFF);
332  outputbuffer[startIndex + 1] = static_cast<char>((value >> 16) & 0xFF);
333  outputbuffer[startIndex + 2] = static_cast<char>((value >> 8) & 0xFF);
334  outputbuffer[startIndex + 3] = static_cast<char>((value ) & 0xFF);
335  break;
336  case BYTE_ORDER_2:
337  outputbuffer[startIndex + 3] = static_cast<char>((value >> 24) & 0xFF);
338  outputbuffer[startIndex + 2] = static_cast<char>((value >> 16) & 0xFF);
339  outputbuffer[startIndex + 1] = static_cast<char>((value >> 8) & 0xFF);
340  outputbuffer[startIndex ] = static_cast<char>((value ) & 0xFF);
341  break;
342  }
343 }
344 
348 void getBytes(int64 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
349 {
350  switch(byteOrder) {
351  case BYTE_ORDER_1:
352  outputbuffer[startIndex ] = static_cast<char>((value >> 56) & 0xFF);
353  outputbuffer[startIndex + 1] = static_cast<char>((value >> 48) & 0xFF);
354  outputbuffer[startIndex + 2] = static_cast<char>((value >> 40) & 0xFF);
355  outputbuffer[startIndex + 3] = static_cast<char>((value >> 32) & 0xFF);
356  outputbuffer[startIndex + 4] = static_cast<char>((value >> 24) & 0xFF);
357  outputbuffer[startIndex + 5] = static_cast<char>((value >> 16) & 0xFF);
358  outputbuffer[startIndex + 6] = static_cast<char>((value >> 8) & 0xFF);
359  outputbuffer[startIndex + 7] = static_cast<char>((value ) & 0xFF);
360  break;
361  case BYTE_ORDER_2:
362  outputbuffer[startIndex + 7] = static_cast<char>((value >> 56) & 0xFF);
363  outputbuffer[startIndex + 6] = static_cast<char>((value >> 48) & 0xFF);
364  outputbuffer[startIndex + 5] = static_cast<char>((value >> 40) & 0xFF);
365  outputbuffer[startIndex + 4] = static_cast<char>((value >> 32) & 0xFF);
366  outputbuffer[startIndex + 3] = static_cast<char>((value >> 24) & 0xFF);
367  outputbuffer[startIndex + 2] = static_cast<char>((value >> 16) & 0xFF);
368  outputbuffer[startIndex + 1] = static_cast<char>((value >> 8) & 0xFF);
369  outputbuffer[startIndex ] = static_cast<char>((value ) & 0xFF);
370  break;
371  }
372 }
373 
377 void getBytes(uint64 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
378 {
379  switch(byteOrder) {
380  case BYTE_ORDER_1:
381  outputbuffer[startIndex ] = static_cast<char>((value >> 56) & 0xFF);
382  outputbuffer[startIndex + 1] = static_cast<char>((value >> 48) & 0xFF);
383  outputbuffer[startIndex + 2] = static_cast<char>((value >> 40) & 0xFF);
384  outputbuffer[startIndex + 3] = static_cast<char>((value >> 32) & 0xFF);
385  outputbuffer[startIndex + 4] = static_cast<char>((value >> 24) & 0xFF);
386  outputbuffer[startIndex + 5] = static_cast<char>((value >> 16) & 0xFF);
387  outputbuffer[startIndex + 6] = static_cast<char>((value >> 8) & 0xFF);
388  outputbuffer[startIndex + 7] = static_cast<char>((value ) & 0xFF);
389  break;
390  case BYTE_ORDER_2:
391  outputbuffer[startIndex + 7] = static_cast<char>((value >> 56) & 0xFF);
392  outputbuffer[startIndex + 6] = static_cast<char>((value >> 48) & 0xFF);
393  outputbuffer[startIndex + 5] = static_cast<char>((value >> 40) & 0xFF);
394  outputbuffer[startIndex + 4] = static_cast<char>((value >> 32) & 0xFF);
395  outputbuffer[startIndex + 3] = static_cast<char>((value >> 24) & 0xFF);
396  outputbuffer[startIndex + 2] = static_cast<char>((value >> 16) & 0xFF);
397  outputbuffer[startIndex + 1] = static_cast<char>((value >> 8) & 0xFF);
398  outputbuffer[startIndex ] = static_cast<char>((value ) & 0xFF);
399  break;
400  }
401 }
402 
406 void getBytes(float32 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
407 {
408  char *c = reinterpret_cast<char*>(&value);
409  int32 i = *reinterpret_cast<int32*>(c);
410  switch(byteOrder) {
411  case BYTE_ORDER_1:
412  getBytes(i, outputbuffer, startIndex, FLOAT_BYTE_ORDER_1);
413  break;
414  case BYTE_ORDER_2:
415  getBytes(i, outputbuffer, startIndex, FLOAT_BYTE_ORDER_2);
416  break;
417  default: ;
418  }
419 }
420 
424 void getBytes(float64 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
425 {
426  char *c = reinterpret_cast<char*>(&value);
427  int64 i = *reinterpret_cast<int64*>(c);
428  switch(byteOrder) {
429  case BYTE_ORDER_1:
430  getBytes(i, outputbuffer, startIndex, FLOAT_BYTE_ORDER_1);
431  break;
432  case BYTE_ORDER_2:
433  getBytes(i, outputbuffer, startIndex, FLOAT_BYTE_ORDER_2);
434  break;
435  default: ;
436  }
437 }
438 
439 }
void getBytes(int16 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
Stores the specified 16-bit signed integer value at a specified position in a char array...
ByteOrder
Specifies the byte order/endianness.
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
float64 toFloat64(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
uint32 toNormalInt(uint32 synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
int16 toInt16(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 16-bit signed integer converted from two bytes at a specified position in a char array...
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
float32 toFloat32(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 32-bit floating point number converted from four bytes at a specified position in a char ar...
uint32 toUInt24(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 32-bit unsigned integer converted from three bytes at a specified position in a char array...
Contains several functions providing conversions between different data types.
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
uint32 toSynchsafeInt(uint32 normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
uint64 toUInt64(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a char array...
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
int32 toInt32(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 32-bit signed integer converted from four bytes at a specified position in a char array...
uint32 toUInt32(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a char array...
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
uint16 toUInt16(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a char array...
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39
int64 toInt64(const char *value, int startIndex, ByteOrder byteOrder)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a char array...