Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6197 in orxonox.OLD


Ignore:
Timestamp:
Dec 20, 2005, 6:18:14 PM (18 years ago)
Author:
bwuest
Message:

converter.h and converter.cc changed: Conversion float to byte* and byte* to float works now.

Location:
branches/network/src
Files:
4 edited

Legend:

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

    r6139 r6197  
    9494    mult *= step;
    9595  }
    96  
    97   printf("tara: %i", result);
    98  
     96
    9997  if (a[INTSIZE - 1] >= sgnadd)
    10098  {
     
    108106}
    109107
    110 /*char* Converter::floatToBinString(float x)
     108/*!
     109 * Converts a float into a string containing its binary representation
     110 */
     111char* Converter::floatToBinString(float x)
    111112{
    112113  char* result = new char[200];
    113114  int pos = 0;
    114115 
    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 
    169 char* Converter::floatToBinString(float x)
    170 {
    171   char* result = new char[200];
    172   int pos = 0;
     116  if (x < 0)
     117  {
     118    result[pos++] = '-';
     119    x = -x;
     120  }
    173121 
    174122  float sub = 1;
    175123  while (sub < x)
    176124    sub *= 2;
    177  
    178   //printf("sub = %f\n", sub);
    179  
    180   //while (sub >= 1 && pos < 200)
     125
    181126  while ((x > 0 || sub >= 1) && pos < 200)
    182127  {
     
    195140  }
    196141 
    197   /*result[pos++] = '.';
    198   sub = 0.5;
    199   while (x > 0 && pos < 200)
     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)
    200174  {
    201175    if (x >= sub)
    202176    {
    203       result[pos] = '1';
     177      mantisse += 1;
    204178      x -= sub;
    205179    }
    206     else
    207       result[pos] = '0';
    208     pos++;
     180   
     181    mantisse *= 2;
     182    exponent--;
    209183    sub /= 2;
    210184  }
    211   */
    212  
    213   return result;
    214 }
     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}
  • branches/network/src/lib/network/converter.h

    r6139 r6197  
    3131    static char* floatToBinString(float x);
    3232   
     33    static byte* floatToByteArray(float x);
     34    static float byteArrayToFloat(byte* a);
     35   
     36    static byte* _floatToByteArray(float x);
     37    static float _byteArrayToFloat(byte* a);
    3338  private:
    3439    Converter();
  • branches/network/src/lib/network/network_game_manager.cc

    r6190 r6197  
    175175void NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
    176176{
    177   BaseObject * b = Factory::fabricate( classID );
     177  //BaseObject * b = Factory::fabricate( classID );
     178  BaseObject * b = NULL;
    178179
    179180  if ( b->isA(CL_SYNCHRONIZEABLE) )
  • branches/network/src/subprojects/network/network_unit_test.cc

    r6139 r6197  
    284284}
    285285
    286 
     286void testFloatConverter(float f)
     287{
     288  char* s = Converter::floatToBinString(f);
     289  printf("%f = ", f);
     290  printf(s); printf("\n");
     291 
     292  byte* res = Converter::floatToByteArray(f);
     293  printf("Byte Array: ");
     294  for (int i = 0; i < 4; i++)
     295    printf("%i  ", res[i]);
     296  printf("\n");
     297 
     298  float b = Converter::byteArrayToFloat(res);
     299  printf("ReConvert: %f \n", b);
     300}
     301
     302void testFloatConverter2(float f)
     303{
     304  char* s = Converter::floatToBinString(f);
     305  printf("### %f = ", f);
     306  printf(s); printf("\n");
     307 
     308  byte* res = Converter::_floatToByteArray(f);
     309  printf("Byte Array: ");
     310  for (int i = 0; i < 4; i++)
     311    printf("%i  ", res[i]);
     312  printf("\n");
     313 
     314  float b = Converter::_byteArrayToFloat(res);
     315  printf("ReConvert: %f \n", b);
     316}
    287317int converter(int argc, char** argv)
    288318{
     319  /*
    289320  int x = 200564786;
    290321  printf("To convert: %i\n", x);
     
    305336  printf("\n");
    306337 
     338  */
     339  /*
    307340  float y;
    308341  char* s;
     
    328361  printf(s); printf("\n");
    329362 
    330  
     363  y = -4.7824f;
     364  s = Converter::floatToBinString(y);
     365  printf("%f = ", y);
     366  printf(s); printf("\n");
     367 
     368  y = -14.35e14f;
     369  s = Converter::floatToBinString(y);
     370  printf("%f = ", y);
     371  printf(s); printf("\n");
     372                                                            */
     373 
     374 
     375  /*
     376  float a = 12.3f;
     377 
     378  char* s = Converter::floatToBinString(a);
     379  printf("%f = ", a);
     380  printf(s); printf("\n");
     381 
     382  byte* res = Converter::floatToByteArray(a);
     383  printf("Byte Array: \n");
     384  for (int i = 0; i < 4; i++)
     385    printf("%i  ", res[i]);
     386  printf("\n");
     387 
     388  float b = Converter::byteArrayToFloat(res);
     389  printf("ReConvert: %f \n", b);
     390  */
     391  testFloatConverter2(12.3f); printf("\n");
     392  testFloatConverter2(134.25f); printf("\n");
     393  testFloatConverter2(35.67e14f); printf("\n");
    331394 
    332395  return 0;
Note: See TracChangeset for help on using the changeset viewer.