Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6108 was 6108, checked in by bwuest, 18 years ago

Converter.h and Converter.cc changed. They should now function for both positive and negative numbers

File size: 2.1 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(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  printf("tara: %i", result);
98 
99  if (a[INTSIZE - 1] >= sgnadd)
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}
Note: See TracBrowser for help on using the repository browser.