Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network_game_manager: implemented some functions

File size: 4.8 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 a byte-array into an int
83 * @param a: The byte-array which is to convert
84 * @return: An int that accords the given byte-array
85 */
86int Converter::byteArrayToInt(const byte* a)
87{
88  int mult = 1;
89  const int step = 256; // = 2 ^ 8
90  int result = 0;
91  for (int i = 0; i < INTSIZE - 1; i++)
92  {
93    result += a[i] * mult;
94    mult *= step;
95  }
96
97  if (a[INTSIZE - 1] >= sgnadd)
98  {
99    result += (a[INTSIZE - 1] - sgnadd) * mult;
100    result *= -1;
101  }
102  else
103    result += a[INTSIZE - 1] * mult;
104
105  return result;
106}
107
108/*!
109 * Converts a float into a string containing its binary representation
110 */
111char* Converter::floatToBinString(float x)
112{
113  char* result = new char[200];
114  int pos = 0;
115
116  if (x < 0)
117  {
118    result[pos++] = '-';
119    x = -x;
120  }
121
122  float sub = 1;
123  while (sub < x)
124    sub *= 2;
125
126  while ((x > 0 || sub >= 1) && pos < 200)
127  {
128    if (x >= sub)
129    {
130      result[pos] = '1';
131      x -= sub;
132    }
133    else
134      result[pos] = '0';
135    pos++;
136    sub /= 2;
137
138    if (sub == 0.5f)
139      result[pos++] = '.';
140  }
141
142  return result;
143}
144
145const int expmult = 8388608; //2^23
146
147/*!
148 * Converts a float value into a byte-array
149 * @param x: The float which is to convert
150 * @return: A byte-array which accords the given float
151 */
152byte* Converter::floatToByteArray(float x)
153{
154  int mantisse = 0;
155  int exponent = 128;
156
157  int sgn;
158  if (x < 0)
159  {
160    x = -x;
161    sgn = -1;
162  }
163  else
164    sgn = 1;
165
166  float sub = 1;
167  while (sub < x)
168  {
169    sub *= 2;
170    exponent++;
171  }
172
173  while (x > 0)
174  {
175    if (x >= sub)
176    {
177      mantisse += 1;
178      x -= sub;
179    }
180
181    mantisse *= 2;
182    exponent--;
183    sub /= 2;
184  }
185  exponent++;
186  mantisse /= 2;
187  while (mantisse < expmult)
188  {
189    mantisse *= 2;
190    exponent--;
191  }
192
193  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
194
195  mantisse -= expmult;
196
197  int hx = mantisse + expmult * exponent;
198  byte* result = intToByteArray(hx);
199  if (sgn == -1)
200    result[3] += sgnadd;
201
202  return result;
203}
204
205
206/*!
207 * Converts a byte-array into a float value
208 * @param a: The byte-array which is to convert
209 * @return: A float value which accords the given byte-array
210 */
211float Converter::byteArrayToFloat(byte* a)
212{
213  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
214  int mantisse = hmant % expmult;
215  mantisse += expmult;
216
217  int hexp = a[2] + a[3] * 256;
218  int exponent = (hexp / 128) % 256 - 128;
219
220  int sgn;
221  if (a[3] >= sgnadd)
222    sgn = -1;
223  else
224    sgn = 1;
225
226  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
227
228  float emult = 1;
229  if (exponent > 0)
230    for (int i = 0; i < exponent; i++)
231      emult *= 2;
232  else if (exponent < 0)
233    for (int i = 0; i > exponent; i--)
234      emult /= 2;
235
236  float result = mantisse * emult;
237  if (sgn == -1)
238    result = -result;
239
240  return result;
241}
242
243/*!
244 * Converts a float value into a byte-array
245 * @param x: The float which is to convert
246 * @return: A byte-array which accords the given float
247 */
248byte* Converter::_floatToByteArray(float x)
249{
250  byte* p = (byte*)&x;
251  byte* result = new byte[4];
252  for (int i = 0; i < 4; i++)
253    result[i] = p[i];
254  return result;
255}
256
257
258/*!
259 * Converts a byte-array into a float value
260 * @param a: The byte-array which is to convert
261 * @return: A float value which accords the given byte-array
262 */
263float Converter::_byteArrayToFloat(byte* a)
264{
265  float* p = (float*)a;
266  float result = *p;
267  return result;
268}
Note: See TracBrowser for help on using the repository browser.