Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/converter.cc @ 6172

Last change on this file since 6172 was 6139, checked in by patrick, 19 years ago

trunk: merged branche network with trunk using command: svn merge -r5999:HEAD, conflicts resolved in favor of the trunk bla

File size: 3.5 KB
RevLine 
[6106]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(byte* a)
87{
88  int mult = 1;
89  const int step = 256; // = 2 ^ 8
90  int result = 0;
[6108]91  for (int i = 0; i < INTSIZE - 1; i++)
[6106]92  {
93    result += a[i] * mult;
94    mult *= step;
95  }
96 
[6108]97  printf("tara: %i", result);
98 
99  if (a[INTSIZE - 1] >= sgnadd)
[6106]100  {
101    result += (a[INTSIZE - 1] - sgnadd) * mult;
102    result *= -1;
103  }
104  else
105    result += a[INTSIZE - 1] * mult;
106 
107  return result;
108}
[6128]109
110/*char* Converter::floatToBinString(float x)
111{
112  char* result = new char[200];
113  int pos = 0;
114 
115  int h = (int)x;
116  if (h > x)
117    h--;
118  x -= h;
119 
120 
121  while (h > 0 && pos < 200)
122  {
123    //printf("%i ", pos);
124   
125    if (h % 2 == 1)
126    {
127      result[pos] = '1';
128      h -= 1;
129    }
130    else
131      result[pos] = '0';
132    pos++;
133    h /= 2;
134  }
135 
136  //printf("x = %f\n", x);
137 
138 
139  //invert
140  for (int i = 0; i < pos / 2; i++)
141  {
142    char temp = result[i];
143    result[i] = result[pos - 1 - i];
144    result[pos - 1 - i] = temp;
145  }
146 
147 
148  result[pos++] = '.';
149  float sub = 0.5;
150  while (x > 0 && pos < 200)
151  {
152    //printf("%i ", pos);
153   
154    if (x >= sub)
155    {
156      result[pos] = '1';
157      x -= sub;
158    }
159    else
160      result[pos] = '0';
161    pos++;
162    sub /= 2;
163  }
164 
165 
166  return result;
167}*/
168
169char* Converter::floatToBinString(float x)
170{
171  char* result = new char[200];
172  int pos = 0;
173 
174  float sub = 1;
175  while (sub < x)
176    sub *= 2;
177 
178  //printf("sub = %f\n", sub);
179 
180  //while (sub >= 1 && pos < 200)
181  while ((x > 0 || sub >= 1) && pos < 200)
182  {
183    if (x >= sub)
184    {
185      result[pos] = '1';
186      x -= sub;
187    }
188    else
189      result[pos] = '0';
190    pos++;
191    sub /= 2;
192   
193    if (sub == 0.5f)
194      result[pos++] = '.';
195  }
196 
197  /*result[pos++] = '.';
198  sub = 0.5;
199  while (x > 0 && pos < 200)
200  {
201    if (x >= sub)
202    {
203      result[pos] = '1';
204      x -= sub;
205    }
206    else
207      result[pos] = '0';
208    pos++;
209    sub /= 2;
210  }
211  */
212 
213  return result;
214}
Note: See TracBrowser for help on using the repository browser.