Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 16, 2005, 1:07:54 PM (18 years ago)
Author:
bwuest
Message:

A function added in class converter.

File:
1 edited

Legend:

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

    r6108 r6128  
    107107  return result;
    108108}
     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 TracChangeset for help on using the changeset viewer.