[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" |
---|
[6634] | 24 | #include "shell_command.h" |
---|
[6106] | 25 | |
---|
[6634] | 26 | #include <limits> |
---|
[6106] | 27 | |
---|
[6634] | 28 | SHELL_COMMAND_STATIC(debug, Converter, Converter::debug); |
---|
| 29 | |
---|
| 30 | |
---|
[6106] | 31 | /* using namespace std is default, this needs to be here */ |
---|
| 32 | using namespace std; |
---|
| 33 | |
---|
| 34 | /*! |
---|
| 35 | * Standard constructor |
---|
| 36 | */ |
---|
| 37 | Converter::Converter() |
---|
| 38 | { |
---|
| 39 | /* set the class id for the base object */ |
---|
| 40 | //this->setClassID(CL_ENTITY_MANAGER, "EntityManager"); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /*! |
---|
| 44 | * Standard destructor |
---|
| 45 | */ |
---|
| 46 | Converter::~Converter() |
---|
| 47 | { |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | const int sgnadd = 128; // = 2^7 |
---|
| 51 | |
---|
| 52 | /*! |
---|
| 53 | * Converts an int into a byte-array |
---|
| 54 | * @remarks: The int is stored in big-endian |
---|
| 55 | * @param x: The int which is to convert |
---|
| 56 | * @return: A byte-array that accords the given int value |
---|
| 57 | */ |
---|
| 58 | byte* Converter::intToByteArray(int x) |
---|
| 59 | { |
---|
| 60 | const int mod = 256; // = 2^8 |
---|
[6341] | 61 | |
---|
| 62 | |
---|
[6106] | 63 | byte* result = new byte[INTSIZE]; |
---|
| 64 | int sgn; |
---|
| 65 | if (x >= 0) |
---|
| 66 | sgn = 1; |
---|
| 67 | else |
---|
| 68 | { |
---|
| 69 | sgn = -1; |
---|
| 70 | x = -x; |
---|
| 71 | } |
---|
[6341] | 72 | |
---|
[6106] | 73 | for (int i = 0; i < INTSIZE; i++) |
---|
| 74 | { |
---|
| 75 | result[i] = x % mod; |
---|
| 76 | x /= mod; |
---|
| 77 | } |
---|
[6341] | 78 | |
---|
[6106] | 79 | if (sgn == -1) |
---|
| 80 | result[INTSIZE - 1] += sgnadd; |
---|
[6341] | 81 | |
---|
| 82 | |
---|
[6106] | 83 | return result; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /*! |
---|
[6341] | 87 | * Converts an int into a byte-array and stores the result into a given byte-array |
---|
| 88 | * @remarks: The int is stored in big-endian |
---|
| 89 | * @param x: The int which is to convert |
---|
[6737] | 90 | * @return: The number of written bytes |
---|
[6341] | 91 | */ |
---|
[6959] | 92 | int Converter::_intToByteArray(int x, byte* a, int length) |
---|
[6341] | 93 | { |
---|
| 94 | if (length < INTSIZE) |
---|
| 95 | { |
---|
| 96 | PRINTF(1)("byte buffer to short to store an int. Needed length %i. Avaiable length %i", INTSIZE, length); |
---|
| 97 | return -1; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | const int mod = 256; // = 2^8 |
---|
| 101 | |
---|
| 102 | int sgn; |
---|
| 103 | if (x >= 0) |
---|
| 104 | sgn = 1; |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | sgn = -1; |
---|
| 108 | x = -x; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | for (int i = 0; i < INTSIZE; i++) |
---|
| 112 | { |
---|
| 113 | a[i] = x % mod; |
---|
| 114 | x /= mod; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (sgn == -1) |
---|
| 118 | a[INTSIZE - 1] += sgnadd; |
---|
| 119 | |
---|
| 120 | return INTSIZE; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /*! |
---|
[6106] | 124 | * Converts a byte-array into an int |
---|
| 125 | * @param a: The byte-array which is to convert |
---|
[6737] | 126 | * @param x: The place where the result is stored |
---|
| 127 | * @return: The number of read bytes |
---|
[6106] | 128 | */ |
---|
[6959] | 129 | int Converter::_byteArrayToInt(const byte* a, int* x) |
---|
[6106] | 130 | { |
---|
| 131 | int mult = 1; |
---|
| 132 | const int step = 256; // = 2 ^ 8 |
---|
[6341] | 133 | *x = 0; |
---|
[6108] | 134 | for (int i = 0; i < INTSIZE - 1; i++) |
---|
[6106] | 135 | { |
---|
[6341] | 136 | *x += a[i] * mult; |
---|
[6106] | 137 | mult *= step; |
---|
| 138 | } |
---|
[6341] | 139 | |
---|
[6108] | 140 | if (a[INTSIZE - 1] >= sgnadd) |
---|
[6106] | 141 | { |
---|
[6341] | 142 | *x += (a[INTSIZE - 1] - sgnadd) * mult; |
---|
| 143 | *x *= -1; |
---|
[6106] | 144 | } |
---|
| 145 | else |
---|
[6341] | 146 | *x += a[INTSIZE - 1] * mult; |
---|
| 147 | |
---|
| 148 | return INTSIZE; |
---|
[6106] | 149 | } |
---|
[6128] | 150 | |
---|
[6959] | 151 | int Converter::byteArrayToInt(const byte* a, int* x) |
---|
| 152 | { |
---|
| 153 | memcpy( x, a, INTSIZE ); |
---|
| 154 | return INTSIZE; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | int Converter::intToByteArray(int x, byte* a, int length) |
---|
| 158 | { |
---|
| 159 | if ( length< INTSIZE ) |
---|
| 160 | { |
---|
[7228] | 161 | PRINTF(1)("Byte Array to small : %d\n", length); |
---|
[6959] | 162 | return 0; |
---|
| 163 | } |
---|
| 164 | memcpy(a, &x, INTSIZE); |
---|
| 165 | return INTSIZE; |
---|
| 166 | } |
---|
| 167 | |
---|
[6341] | 168 | /*! |
---|
| 169 | * Converts a float into a string containing its binary representation |
---|
[6737] | 170 | * @param x: The float which is to convert |
---|
| 171 | * @return: A string containing the float's binary representation |
---|
[6341] | 172 | */ |
---|
| 173 | char* Converter::floatToBinString(float x) |
---|
[6128] | 174 | { |
---|
| 175 | char* result = new char[200]; |
---|
| 176 | int pos = 0; |
---|
[6341] | 177 | |
---|
| 178 | if (x < 0) |
---|
[6128] | 179 | { |
---|
[6341] | 180 | result[pos++] = '-'; |
---|
| 181 | x = -x; |
---|
[6128] | 182 | } |
---|
[6341] | 183 | |
---|
| 184 | float sub = 1; |
---|
| 185 | while (sub < x) |
---|
| 186 | sub *= 2; |
---|
| 187 | |
---|
| 188 | while ((x > 0 || sub >= 1) && pos < 200) |
---|
[6128] | 189 | { |
---|
| 190 | if (x >= sub) |
---|
| 191 | { |
---|
| 192 | result[pos] = '1'; |
---|
| 193 | x -= sub; |
---|
| 194 | } |
---|
| 195 | else |
---|
| 196 | result[pos] = '0'; |
---|
| 197 | pos++; |
---|
| 198 | sub /= 2; |
---|
[6341] | 199 | |
---|
| 200 | if (sub == 0.5f) |
---|
| 201 | result[pos++] = '.'; |
---|
[6128] | 202 | } |
---|
[6341] | 203 | |
---|
[6128] | 204 | return result; |
---|
[6341] | 205 | } |
---|
[6128] | 206 | |
---|
[6341] | 207 | const int expmult = 8388608; //2^23 |
---|
| 208 | |
---|
[6634] | 209 | float Converter::getDenormConst() |
---|
| 210 | { |
---|
| 211 | const int exp = 126; |
---|
| 212 | float result = 1.0f; |
---|
| 213 | for (int i = 0; i < exp; i++) |
---|
| 214 | result /= 2.0f; |
---|
| 215 | return result; |
---|
| 216 | } |
---|
| 217 | |
---|
[6341] | 218 | /*! |
---|
| 219 | * Converts a float value into a byte-array and stores the result into a given byte-array |
---|
| 220 | * @param x: The float which is to convert |
---|
[6737] | 221 | * @param a: The byte array where the result is to store |
---|
| 222 | * @param length: The length of the array a |
---|
| 223 | * @return: The number of written bytes |
---|
[6341] | 224 | */ |
---|
[6753] | 225 | int Converter::_floatToByteArray(float x, byte* a, int length) |
---|
[6341] | 226 | { |
---|
| 227 | if (length < FLOATSIZE) |
---|
| 228 | { |
---|
| 229 | PRINTF(1)("byte buffer to short to store a float. Needed length %i. Avaiable length %i", FLOATSIZE, length); |
---|
| 230 | return -1; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | //handle 0 else function will loop for ever |
---|
[6634] | 234 | /*if ( x == 0 ) |
---|
[6341] | 235 | { |
---|
| 236 | for ( int i = 0; i<FLOATSIZE; i++) |
---|
| 237 | a[i] = 0; |
---|
| 238 | return FLOATSIZE; |
---|
[6634] | 239 | }*/ |
---|
[6341] | 240 | |
---|
| 241 | int mantisse = 0; |
---|
| 242 | int exponent = 128; |
---|
| 243 | |
---|
| 244 | int sgn; |
---|
| 245 | if (x < 0) |
---|
| 246 | { |
---|
| 247 | x = -x; |
---|
| 248 | sgn = -1; |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | sgn = 1; |
---|
| 252 | |
---|
[6634] | 253 | if (x == 0) |
---|
[6341] | 254 | { |
---|
[6634] | 255 | exponent = 255; |
---|
| 256 | mantisse = 0; |
---|
[6341] | 257 | } |
---|
[6634] | 258 | else |
---|
| 259 | { |
---|
| 260 | //if (x < getDenormConst()) |
---|
| 261 | // printf("Denormalisiert!\n"); |
---|
| 262 | //printf("DenormConst = %e", getDenormConst()); |
---|
[6341] | 263 | |
---|
[6634] | 264 | float sub = 1; |
---|
| 265 | while (sub < x) |
---|
[6128] | 266 | { |
---|
[6634] | 267 | sub *= 2; |
---|
| 268 | exponent++; |
---|
[6128] | 269 | } |
---|
[6341] | 270 | |
---|
[6634] | 271 | while (x > 0) |
---|
| 272 | { |
---|
| 273 | if (x >= sub) |
---|
| 274 | { |
---|
| 275 | mantisse += 1; |
---|
| 276 | x -= sub; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | mantisse *= 2; |
---|
| 280 | exponent--; |
---|
| 281 | sub /= 2; |
---|
| 282 | } |
---|
| 283 | exponent++; |
---|
| 284 | mantisse /= 2; |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | /// printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent); |
---|
| 288 | |
---|
| 289 | |
---|
[6737] | 290 | //if (mantisse != 0) |
---|
| 291 | //{ |
---|
[6634] | 292 | while (mantisse < expmult) |
---|
| 293 | { |
---|
| 294 | mantisse *= 2; |
---|
| 295 | exponent--; |
---|
| 296 | } |
---|
| 297 | |
---|
[6737] | 298 | if (exponent >= 0) |
---|
| 299 | mantisse -= expmult; |
---|
| 300 | else |
---|
| 301 | { |
---|
| 302 | //Denormalized |
---|
| 303 | while (exponent < 0) |
---|
| 304 | { |
---|
| 305 | mantisse /= 2; |
---|
| 306 | exponent++; |
---|
| 307 | } |
---|
| 308 | printf("Conv: Denorm"); |
---|
| 309 | } |
---|
| 310 | //} |
---|
[6128] | 311 | } |
---|
[6341] | 312 | |
---|
[6634] | 313 | /// printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent); |
---|
[6341] | 314 | |
---|
| 315 | |
---|
| 316 | int hx = mantisse + expmult * exponent; |
---|
[6737] | 317 | //int result = intToByteArray(hx, a, length); |
---|
| 318 | intToByteArray(hx, a, length); |
---|
[6341] | 319 | if (sgn == -1) |
---|
| 320 | a[3] += sgnadd; |
---|
| 321 | |
---|
[6634] | 322 | |
---|
| 323 | // int hx = mantisse + expmult * exponent; |
---|
| 324 | // byte* result = intToByteArray(hx); |
---|
| 325 | // if (sgn == -1) |
---|
| 326 | // result[3] += sgnadd; |
---|
| 327 | |
---|
[6737] | 328 | //return result; |
---|
| 329 | return FLOATSIZE; |
---|
[6128] | 330 | } |
---|
[6341] | 331 | |
---|
| 332 | |
---|
| 333 | /*! |
---|
| 334 | * Converts a byte-array into a float value |
---|
| 335 | * @param a: The byte-array which is to convert |
---|
[6737] | 336 | * @param x: The place where the result is to store |
---|
| 337 | * @return: The number of read bytes |
---|
[6341] | 338 | */ |
---|
[6753] | 339 | int Converter::_byteArrayToFloat(const byte* a, float* x) |
---|
[6341] | 340 | { |
---|
[6634] | 341 | //handle 0 |
---|
| 342 | /*for (int i = 0; i<FLOATSIZE; i++) |
---|
[6341] | 343 | { |
---|
| 344 | if (a[i]!=0) |
---|
| 345 | break; |
---|
| 346 | if ( i==FLOATSIZE-1 ) |
---|
| 347 | { |
---|
| 348 | *x = 0.0f; |
---|
| 349 | return FLOATSIZE; |
---|
| 350 | } |
---|
[6634] | 351 | }*/ |
---|
[6341] | 352 | |
---|
[6634] | 353 | int hexp = a[2] + a[3] * 256; |
---|
| 354 | int exponent = (hexp / 128) % 256; |
---|
[6341] | 355 | |
---|
| 356 | int hmant = a[0] + a[1] * 256 + a[2] * 65536; |
---|
| 357 | int mantisse = hmant % expmult; |
---|
[6634] | 358 | |
---|
| 359 | //handle 0 |
---|
| 360 | if (mantisse == 0 && exponent == 255) |
---|
| 361 | { |
---|
| 362 | *x = 0; |
---|
| 363 | return FLOATSIZE; |
---|
| 364 | } |
---|
[6737] | 365 | else if (exponent == 0 && mantisse != 0) |
---|
| 366 | { |
---|
| 367 | exponent = -126; |
---|
| 368 | printf("ReConv: Denorm"); |
---|
| 369 | } |
---|
| 370 | else |
---|
| 371 | { |
---|
| 372 | mantisse += expmult; |
---|
| 373 | exponent -= 128; |
---|
| 374 | } |
---|
[6634] | 375 | |
---|
[6341] | 376 | int sgn; |
---|
| 377 | if (a[3] >= sgnadd) |
---|
| 378 | sgn = -1; |
---|
| 379 | else |
---|
| 380 | sgn = 1; |
---|
| 381 | |
---|
[6634] | 382 | /// printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent); |
---|
[6341] | 383 | |
---|
| 384 | float emult = 1; |
---|
| 385 | if (exponent > 0) |
---|
| 386 | for (int i = 0; i < exponent; i++) |
---|
| 387 | emult *= 2; |
---|
| 388 | else if (exponent < 0) |
---|
| 389 | for (int i = 0; i > exponent; i--) |
---|
| 390 | emult /= 2; |
---|
| 391 | |
---|
[6634] | 392 | /* |
---|
| 393 | float result = mantisse * emult; |
---|
| 394 | if (sgn == -1) |
---|
| 395 | result = -result; |
---|
| 396 | |
---|
| 397 | return result; |
---|
| 398 | */ |
---|
| 399 | |
---|
[6341] | 400 | *x = mantisse * emult; |
---|
| 401 | if (sgn == -1) |
---|
| 402 | *x = - *x; |
---|
| 403 | |
---|
| 404 | return FLOATSIZE; |
---|
| 405 | } |
---|
| 406 | |
---|
[6634] | 407 | /*! |
---|
| 408 | * Converts a float value into a byte-array |
---|
| 409 | * @param x: The float which is to convert |
---|
[6737] | 410 | * @param a: The array where the result is to store |
---|
| 411 | * @param length: The length of the array a |
---|
| 412 | * @return: The number of written bytes |
---|
[6634] | 413 | */ |
---|
[6753] | 414 | int Converter::floatToByteArray(float x, byte* a, int length) |
---|
[6634] | 415 | { |
---|
[6737] | 416 | if ( length< FLOATSIZE ) |
---|
[6634] | 417 | { |
---|
| 418 | PRINTF(1)("Byte Array to small\n"); |
---|
| 419 | return 0; |
---|
| 420 | } |
---|
| 421 | byte* p = (byte*)&x; |
---|
| 422 | |
---|
| 423 | for (int i = 0; i < 4; i++) |
---|
| 424 | a[i] = p[i]; |
---|
[6753] | 425 | |
---|
[6737] | 426 | return FLOATSIZE; |
---|
[6634] | 427 | } |
---|
| 428 | |
---|
| 429 | |
---|
| 430 | /*! |
---|
| 431 | * Converts a byte-array into a float value |
---|
| 432 | * @param a: The byte-array which is to convert |
---|
[6737] | 433 | * @param x: The place where the result is to store |
---|
| 434 | * @return: The number of read bytes |
---|
[6634] | 435 | */ |
---|
[6753] | 436 | int Converter::byteArrayToFloat(const byte* a, float* x) |
---|
[6634] | 437 | { |
---|
| 438 | *x = *((float*)a); |
---|
| 439 | |
---|
[6737] | 440 | return FLOATSIZE; |
---|
[6634] | 441 | } |
---|
| 442 | |
---|
[6737] | 443 | |
---|
| 444 | |
---|
| 445 | |
---|
| 446 | |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | |
---|
| 450 | |
---|
| 451 | |
---|
[6341] | 452 | /** |
---|
| 453 | * copies a strint to a byte array |
---|
| 454 | * @param s: string to copy |
---|
| 455 | * @param a: byte array |
---|
| 456 | * @param length: string length |
---|
| 457 | * @return: the used number of bytes in byte array |
---|
| 458 | */ |
---|
[7228] | 459 | int Converter::stringToByteArray( const std::string & s, byte * a, int maxLength ) |
---|
[6341] | 460 | { |
---|
[7228] | 461 | if ( s.length()+INTSIZE > maxLength ) |
---|
[6341] | 462 | { |
---|
[7228] | 463 | PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, s.length()+INTSIZE); |
---|
[6341] | 464 | return -1; |
---|
| 465 | } |
---|
| 466 | |
---|
[7228] | 467 | int n = Converter::intToByteArray( s.length(), a, maxLength ); |
---|
[6341] | 468 | |
---|
[7228] | 469 | memcpy( a+INTSIZE, s.c_str(), s.length() ); |
---|
[6341] | 470 | |
---|
[7228] | 471 | return s.length() + INTSIZE; |
---|
[6341] | 472 | } |
---|
| 473 | |
---|
| 474 | /** |
---|
| 475 | * reads a string out of a byte array |
---|
| 476 | * @param a: byte array |
---|
| 477 | * @param s: string |
---|
| 478 | * @param maxLength: max bytes to copy |
---|
| 479 | * @return: the number of read bytes in byte array |
---|
| 480 | */ |
---|
[7228] | 481 | int Converter::byteArrayToString( const byte * a, std::string&s, int maxLength ) |
---|
[6341] | 482 | { |
---|
| 483 | int length; |
---|
| 484 | |
---|
| 485 | int n = Converter::byteArrayToInt( a, &length ); |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | if ( length+1 > maxLength ) |
---|
| 489 | { |
---|
[7228] | 490 | PRINTF(1)("something went wrong length > remaining bytes in buffer\n" ); |
---|
| 491 | s = ""; |
---|
[6341] | 492 | return -1; |
---|
| 493 | } |
---|
| 494 | |
---|
[7228] | 495 | s[0] = '\0'; |
---|
| 496 | s.append( (char*)a+n, length ); |
---|
[6341] | 497 | |
---|
| 498 | return n+length; |
---|
| 499 | } |
---|
| 500 | |
---|
[7228] | 501 | #if 0 |
---|
[6341] | 502 | /** |
---|
| 503 | * reads a string out of a byte array and allocates memory for string |
---|
| 504 | * @param a: byte array |
---|
| 505 | * @param s: string |
---|
| 506 | * @param maxLength: max bytes to copy |
---|
| 507 | * @return: the number of read bytes in byte array |
---|
| 508 | */ |
---|
| 509 | int Converter::byteArrayToStringM( const byte * a, char*& s ) |
---|
| 510 | { |
---|
| 511 | int length; |
---|
| 512 | |
---|
| 513 | int n = Converter::byteArrayToInt( a, &length ); |
---|
| 514 | |
---|
| 515 | s = new char[length+1]; |
---|
| 516 | |
---|
| 517 | if ( !s ) |
---|
| 518 | { |
---|
| 519 | PRINTF(1)("Could not allocate memory!\n" ); |
---|
| 520 | return -1; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | memcpy( s, a+n, length ); |
---|
| 524 | s[length] = '\0'; |
---|
| 525 | |
---|
| 526 | return n+length; |
---|
| 527 | } |
---|
[7228] | 528 | #endif |
---|
[6341] | 529 | |
---|
[6634] | 530 | |
---|
| 531 | |
---|
| 532 | void Converter::floatTest(float x) |
---|
| 533 | { |
---|
| 534 | //float x = 8.0f; |
---|
| 535 | //float x = numeric_limits<float>::infinity(); |
---|
| 536 | |
---|
| 537 | printf("To Convert: %e\n", x); |
---|
| 538 | |
---|
[6753] | 539 | // byte* res = floatToByteArray(x); |
---|
| 540 | // for (int i = 0; i < 4; i++) |
---|
| 541 | // printf("%i ", res[i]); |
---|
| 542 | // printf("\n"); |
---|
[6634] | 543 | |
---|
[6753] | 544 | // float y = byteArrayToFloat(res); |
---|
| 545 | // printf("ReConvert: %e\n", y); |
---|
[6634] | 546 | |
---|
[6753] | 547 | // if (x == y) |
---|
| 548 | // printf("equal\n"); |
---|
| 549 | // else |
---|
| 550 | // printf("different\n"); |
---|
[6634] | 551 | } |
---|
| 552 | |
---|
[6737] | 553 | void Converter::ArrayfloatTest(float x) |
---|
| 554 | { |
---|
| 555 | //float x = 8.0f; |
---|
| 556 | //float x = numeric_limits<float>::infinity(); |
---|
| 557 | |
---|
| 558 | printf("To Convert: %e\n", x); |
---|
| 559 | byte* res = new byte[4]; |
---|
| 560 | |
---|
| 561 | int wr = floatToByteArray(x, res, 4); |
---|
| 562 | for (int i = 0; i < 4; i++) |
---|
| 563 | printf("%i ", res[i]); |
---|
| 564 | printf(" written bytes: %i \n", wr); |
---|
| 565 | |
---|
| 566 | float y; |
---|
| 567 | int rd = byteArrayToFloat(res, &y); |
---|
| 568 | printf("ReConvert: %e\n", y); |
---|
| 569 | printf("Read bytes: %i -> ", rd); |
---|
| 570 | |
---|
| 571 | if (x == y) |
---|
| 572 | printf("equal\n"); |
---|
| 573 | else |
---|
| 574 | printf("different\n"); |
---|
| 575 | } |
---|
| 576 | |
---|
[6634] | 577 | void Converter::debug() |
---|
| 578 | { |
---|
| 579 | printf("We rulez\n"); |
---|
| 580 | |
---|
[6737] | 581 | ArrayfloatTest(0.125f); |
---|
| 582 | ArrayfloatTest(64.0f); |
---|
| 583 | ArrayfloatTest(0.0f); |
---|
| 584 | ArrayfloatTest(5.00091e-29f); |
---|
[6634] | 585 | |
---|
| 586 | //floatTest(-18.0098f); |
---|
| 587 | //floatTest(-24.07e23f); |
---|
| 588 | //floatTest(-0.0f); |
---|
| 589 | //floatTest(-5.67e-29f); |
---|
[6753] | 590 | |
---|
| 591 | |
---|
[6737] | 592 | //floatTest(-45.7e-32f); |
---|
| 593 | //floatTest(45.7e-33f); |
---|
| 594 | //floatTest(-45.7e-34f); |
---|
| 595 | //floatTest(45.7e-35f); |
---|
[6634] | 596 | } |
---|