Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/converter.cc @ 6273

Last change on this file since 6273 was 6273, checked in by rennerc, 18 years ago

converter: added functions for strings
network_protocol: length and id are now int
network_game_manager: fixed some more bugs :D
skybox: is loaded on client corectly now :)

File size: 8.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Wuest
13   co-programmer: ...
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
22/* include your own header */
23#include "converter.h"
24
25
26/* using namespace std is default, this needs to be here */
27using namespace std;
28
29/*!
30 * Standard constructor
31 */
32Converter::Converter()
33{
34  /* set the class id for the base object */
35  //this->setClassID(CL_ENTITY_MANAGER, "EntityManager");
36}
37
38/*!
39 * Standard destructor
40 */
41Converter::~Converter()
42{
43}
44
45const int sgnadd = 128; // = 2^7
46
47/*!
48 * Converts an int into a byte-array
49 * @remarks: The int is stored in big-endian
50 * @param x: The int which is to convert
51 * @return: A byte-array that accords the given int value
52 */
53byte* Converter::intToByteArray(int x)
54{
55  const int mod = 256; // = 2^8
56
57
58  byte* result = new byte[INTSIZE];
59  int sgn;
60  if (x >= 0)
61    sgn = 1;
62  else
63  {
64    sgn = -1;
65    x = -x;
66  }
67
68  for (int i = 0; i < INTSIZE; i++)
69  {
70    result[i] = x % mod;
71    x /= mod;
72  }
73
74  if (sgn == -1)
75    result[INTSIZE - 1] += sgnadd;
76
77
78  return result;
79}
80
81/*!
82 * Converts an int into a byte-array and stores the result into a given byte-array
83 * @remarks: The int is stored in big-endian
84 * @param x: The int which is to convert
85 * @return: A byte-array that accords the given int value
86 */
87int Converter::intToByteArray(int x, byte* a, int length)
88{
89  if (length < INTSIZE)
90  {
91    PRINTF(1)("byte buffer to short to store an int. Needed length %i. Avaiable length %i", INTSIZE, length);
92    return -1;
93  }
94
95  const int mod = 256; // = 2^8
96
97  int sgn;
98  if (x >= 0)
99    sgn = 1;
100  else
101  {
102    sgn = -1;
103    x = -x;
104  }
105
106  for (int i = 0; i < INTSIZE; i++)
107  {
108    a[i] = x % mod;
109    x /= mod;
110  }
111
112  if (sgn == -1)
113    a[INTSIZE - 1] += sgnadd;
114
115  return INTSIZE;
116}
117
118/*!
119 * Converts a byte-array into an int
120 * @param a: The byte-array which is to convert
121 * @return: An int that accords the given byte-array
122 */
123int Converter::byteArrayToInt(const byte* a, int* x)
124{
125  int mult = 1;
126  const int step = 256; // = 2 ^ 8
127  *x = 0;
128  for (int i = 0; i < INTSIZE - 1; i++)
129  {
130    *x += a[i] * mult;
131    mult *= step;
132  }
133
134  if (a[INTSIZE - 1] >= sgnadd)
135  {
136    *x += (a[INTSIZE - 1] - sgnadd) * mult;
137    *x *= -1;
138  }
139  else
140    *x += a[INTSIZE - 1] * mult;
141
142  return INTSIZE;
143}
144
145/*!
146 * Converts a float into a string containing its binary representation
147 */
148char* Converter::floatToBinString(float x)
149{
150  char* result = new char[200];
151  int pos = 0;
152
153  if (x < 0)
154  {
155    result[pos++] = '-';
156    x = -x;
157  }
158
159  float sub = 1;
160  while (sub < x)
161    sub *= 2;
162
163  while ((x > 0 || sub >= 1) && pos < 200)
164  {
165    if (x >= sub)
166    {
167      result[pos] = '1';
168      x -= sub;
169    }
170    else
171      result[pos] = '0';
172    pos++;
173    sub /= 2;
174
175    if (sub == 0.5f)
176      result[pos++] = '.';
177  }
178
179  return result;
180}
181
182const int expmult = 8388608; //2^23
183
184/*!
185 * Converts a float value into a byte-array
186 * @param x: The float which is to convert
187 * @return: A byte-array which accords the given float
188 */
189byte* Converter::floatToByteArray(float x)
190{
191  int mantisse = 0;
192  int exponent = 128;
193
194  int sgn;
195  if (x < 0)
196  {
197    x = -x;
198    sgn = -1;
199  }
200  else
201    sgn = 1;
202
203  float sub = 1;
204  while (sub < x)
205  {
206    sub *= 2;
207    exponent++;
208  }
209
210  while (x > 0)
211  {
212    if (x >= sub)
213    {
214      mantisse += 1;
215      x -= sub;
216    }
217
218    mantisse *= 2;
219    exponent--;
220    sub /= 2;
221  }
222  exponent++;
223  mantisse /= 2;
224  while (mantisse < expmult)
225  {
226    mantisse *= 2;
227    exponent--;
228  }
229
230  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
231
232  mantisse -= expmult;
233
234  int hx = mantisse + expmult * exponent;
235  byte* result = intToByteArray(hx);
236  if (sgn == -1)
237    result[3] += sgnadd;
238
239  return result;
240}
241
242
243/*!
244 * Converts a byte-array into a float value
245 * @param a: The byte-array which is to convert
246 * @return: A float value which accords the given byte-array
247 */
248float Converter::byteArrayToFloat(byte* a)
249{
250  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
251  int mantisse = hmant % expmult;
252  mantisse += expmult;
253
254  int hexp = a[2] + a[3] * 256;
255  int exponent = (hexp / 128) % 256 - 128;
256
257  int sgn;
258  if (a[3] >= sgnadd)
259    sgn = -1;
260  else
261    sgn = 1;
262
263  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
264
265  float emult = 1;
266  if (exponent > 0)
267    for (int i = 0; i < exponent; i++)
268      emult *= 2;
269  else if (exponent < 0)
270    for (int i = 0; i > exponent; i--)
271      emult /= 2;
272
273  float result = mantisse * emult;
274  if (sgn == -1)
275    result = -result;
276
277  return result;
278}
279
280/*!
281 * Converts a float value into a byte-array
282 * @param x: The float which is to convert
283 * @return: A byte-array which accords the given float
284 */
285byte* Converter::_floatToByteArray(float x)
286{
287  byte* p = (byte*)&x;
288  byte* result = new byte[4];
289  for (int i = 0; i < 4; i++)
290    result[i] = p[i];
291  return result;
292}
293
294
295/*!
296 * Converts a byte-array into a float value
297 * @param a: The byte-array which is to convert
298 * @return: A float value which accords the given byte-array
299 */
300float Converter::_byteArrayToFloat(byte* a)
301{
302  float* p = (float*)a;
303  float result = *p;
304  return result;
305}
306
307/*!
308 * Converts a float value into a byte-array and stores the result into a given byte-array
309 * @param x: The float which is to convert
310 * @return: A byte-array which accords the given float
311 */
312int Converter::floatToByteArray(float x, byte* a, int length)
313{
314  if (length < FLOATSIZE)
315  {
316    PRINTF(1)("byte buffer to short to store a float. Needed length %i. Avaiable length %i", FLOATSIZE, length);
317    return -1;
318  }
319  int mantisse = 0;
320  int exponent = 128;
321
322  int sgn;
323  if (x < 0)
324  {
325    x = -x;
326    sgn = -1;
327  }
328  else
329    sgn = 1;
330
331  float sub = 1;
332  while (sub < x)
333  {
334    sub *= 2;
335    exponent++;
336  }
337
338  while (x > 0)
339  {
340    if (x >= sub)
341    {
342      mantisse += 1;
343      x -= sub;
344    }
345
346    mantisse *= 2;
347    exponent--;
348    sub /= 2;
349  }
350  exponent++;
351  mantisse /= 2;
352  while (mantisse < expmult)
353  {
354    mantisse *= 2;
355    exponent--;
356  }
357
358  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
359
360  mantisse -= expmult;
361
362  int hx = mantisse + expmult * exponent;
363  int result = intToByteArray(hx, a, length);
364  if (sgn == -1)
365    a[3] += sgnadd;
366
367  return result;
368}
369
370
371/*!
372 * Converts a byte-array into a float value
373 * @param a: The byte-array which is to convert
374 * @return: A float value which accords the given byte-array
375 */
376int Converter::byteArrayToFloat(const byte* a, float* x)
377{
378  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
379  int mantisse = hmant % expmult;
380  mantisse += expmult;
381
382  int hexp = a[2] + a[3] * 256;
383  int exponent = (hexp / 128) % 256 - 128;
384
385  int sgn;
386  if (a[3] >= sgnadd)
387    sgn = -1;
388  else
389    sgn = 1;
390
391  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
392
393  float emult = 1;
394  if (exponent > 0)
395    for (int i = 0; i < exponent; i++)
396      emult *= 2;
397  else if (exponent < 0)
398    for (int i = 0; i > exponent; i--)
399      emult /= 2;
400
401  *x = mantisse * emult;
402  if (sgn == -1)
403    *x = -  *x;
404
405  return FLOATSIZE;
406}
407
408/**
409 * copies a strint to a byte array
410 * @param s: string to copy
411 * @param a: byte array
412 * @param length: string length
413 * @return: the used number of bytes in byte array
414 */
415int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength )
416{
417  if ( length+INTSIZE > maxLength )
418  {
419    PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE);
420    return -1;
421  }
422
423  int n = Converter::intToByteArray( length, a, maxLength );
424
425  memcpy( a+INTSIZE, s, length );
426
427  return length + INTSIZE;
428}
429
430/**
431 * reads a string out of a byte array
432 * @param a: byte array
433 * @param s: string
434 * @param maxLength: max bytes to copy
435 * @return: the number of read bytes in byte array
436 */
437int Converter::byteArrayToString( const byte * a, char * s, int maxLength )
438{
439  int length;
440
441  int n = Converter::byteArrayToInt( a, &length );
442
443  if ( length+1 > maxLength )
444  {
445    PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 );
446    return -1;
447  }
448
449  memcpy( s, a+n, length );
450  s[length] = '\0';
451
452  return n+length;
453}
Note: See TracBrowser for help on using the repository browser.