Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6228 in orxonox.OLD


Ignore:
Timestamp:
Dec 21, 2005, 2:50:20 PM (18 years ago)
Author:
bwuest
Message:

Take my converter.h again!

Location:
branches/network/src/lib/network
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/converter.cc

    r6219 r6228  
    8080
    8181/*!
     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/*!
    82119 * Converts a byte-array into an int
    83120 * @param a: The byte-array which is to convert
    84121 * @return: An int that accords the given byte-array
    85122 */
    86 int Converter::byteArrayToInt(const byte* a)
     123int Converter::byteArrayToInt(const byte* a, int* x)
    87124{
    88125  int mult = 1;
    89126  const int step = 256; // = 2 ^ 8
    90   int result = 0;
     127  *x = 0;
    91128  for (int i = 0; i < INTSIZE - 1; i++)
    92129  {
    93     result += a[i] * mult;
     130    *x += a[i] * mult;
    94131    mult *= step;
    95132  }
     
    97134  if (a[INTSIZE - 1] >= sgnadd)
    98135  {
    99     result += (a[INTSIZE - 1] - sgnadd) * mult;
    100     result *= -1;
    101   }
    102   else
    103     result += a[INTSIZE - 1] * mult;
    104 
    105   return result;
     136    *x += (a[INTSIZE - 1] - sgnadd) * mult;
     137    *x *= -1;
     138  }
     139  else
     140    *x += a[INTSIZE - 1] * mult;
     141
     142  return INTSIZE;
    106143}
    107144
     
    267304  return result;
    268305}
     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(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}
  • branches/network/src/lib/network/converter.h

    r6225 r6228  
    1515/* The size of an int in byte */
    1616#define INTSIZE 4
     17/* The size of a float in byte */
     18#define FLOATSIZE 4
    1719
    1820/*!
     
    2628
    2729    static int intToByteArray(int x, byte* a, int length);
    28     static int byteArrayToInt(byte* a, int* x);
     30    static int byteArrayToInt(const byte* a, int* x);
    2931
    30     static int floatToByteArray(float x, byte* a, int length);
    31     static int byteArrayToFloat(byte* a, float* x);
     32    static int floatToByteArray(float x, const byte* a, int length);
     33    static int byteArrayToFloat(const byte* a, float* x);
    3234   
    3335    //Test
Note: See TracChangeset for help on using the changeset viewer.