[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 | { |
---|
| 161 | PRINTF(1)("Byte Array to small\n"); |
---|
| 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 | */ |
---|
| 459 | int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength ) |
---|
| 460 | { |
---|
| 461 | if ( length+INTSIZE > maxLength ) |
---|
| 462 | { |
---|
| 463 | PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE); |
---|
| 464 | return -1; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | int n = Converter::intToByteArray( length, a, maxLength ); |
---|
| 468 | |
---|
| 469 | memcpy( a+INTSIZE, s, length ); |
---|
| 470 | |
---|
| 471 | return length + INTSIZE; |
---|
| 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 | */ |
---|
| 481 | int Converter::byteArrayToString( const byte * a, char * s, int maxLength ) |
---|
| 482 | { |
---|
| 483 | int length; |
---|
| 484 | |
---|
| 485 | int n = Converter::byteArrayToInt( a, &length ); |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | if ( length+1 > maxLength ) |
---|
| 489 | { |
---|
| 490 | PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 ); |
---|
| 491 | strncpy(s,"",maxLength); |
---|
| 492 | return -1; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | memcpy( s, a+n, length ); |
---|
| 496 | s[length] = '\0'; |
---|
| 497 | |
---|
| 498 | return n+length; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | /** |
---|
| 502 | * reads a string out of a byte array and allocates memory for string |
---|
| 503 | * @param a: byte array |
---|
| 504 | * @param s: string |
---|
| 505 | * @param maxLength: max bytes to copy |
---|
| 506 | * @return: the number of read bytes in byte array |
---|
| 507 | */ |
---|
| 508 | int Converter::byteArrayToStringM( const byte * a, char*& s ) |
---|
| 509 | { |
---|
| 510 | int length; |
---|
| 511 | |
---|
| 512 | int n = Converter::byteArrayToInt( a, &length ); |
---|
| 513 | |
---|
| 514 | s = new char[length+1]; |
---|
| 515 | |
---|
| 516 | if ( !s ) |
---|
| 517 | { |
---|
| 518 | PRINTF(1)("Could not allocate memory!\n" ); |
---|
| 519 | return -1; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | memcpy( s, a+n, length ); |
---|
| 523 | s[length] = '\0'; |
---|
| 524 | |
---|
| 525 | return n+length; |
---|
| 526 | } |
---|
| 527 | |
---|
[6634] | 528 | |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | void Converter::floatTest(float x) |
---|
| 532 | { |
---|
| 533 | //float x = 8.0f; |
---|
| 534 | //float x = numeric_limits<float>::infinity(); |
---|
| 535 | |
---|
| 536 | printf("To Convert: %e\n", x); |
---|
| 537 | |
---|
[6753] | 538 | // byte* res = floatToByteArray(x); |
---|
| 539 | // for (int i = 0; i < 4; i++) |
---|
| 540 | // printf("%i ", res[i]); |
---|
| 541 | // printf("\n"); |
---|
[6634] | 542 | |
---|
[6753] | 543 | // float y = byteArrayToFloat(res); |
---|
| 544 | // printf("ReConvert: %e\n", y); |
---|
[6634] | 545 | |
---|
[6753] | 546 | // if (x == y) |
---|
| 547 | // printf("equal\n"); |
---|
| 548 | // else |
---|
| 549 | // printf("different\n"); |
---|
[6634] | 550 | } |
---|
| 551 | |
---|
[6737] | 552 | void Converter::ArrayfloatTest(float x) |
---|
| 553 | { |
---|
| 554 | //float x = 8.0f; |
---|
| 555 | //float x = numeric_limits<float>::infinity(); |
---|
| 556 | |
---|
| 557 | printf("To Convert: %e\n", x); |
---|
| 558 | byte* res = new byte[4]; |
---|
| 559 | |
---|
| 560 | int wr = floatToByteArray(x, res, 4); |
---|
| 561 | for (int i = 0; i < 4; i++) |
---|
| 562 | printf("%i ", res[i]); |
---|
| 563 | printf(" written bytes: %i \n", wr); |
---|
| 564 | |
---|
| 565 | float y; |
---|
| 566 | int rd = byteArrayToFloat(res, &y); |
---|
| 567 | printf("ReConvert: %e\n", y); |
---|
| 568 | printf("Read bytes: %i -> ", rd); |
---|
| 569 | |
---|
| 570 | if (x == y) |
---|
| 571 | printf("equal\n"); |
---|
| 572 | else |
---|
| 573 | printf("different\n"); |
---|
| 574 | } |
---|
| 575 | |
---|
[6634] | 576 | void Converter::debug() |
---|
| 577 | { |
---|
| 578 | printf("We rulez\n"); |
---|
| 579 | |
---|
[6737] | 580 | ArrayfloatTest(0.125f); |
---|
| 581 | ArrayfloatTest(64.0f); |
---|
| 582 | ArrayfloatTest(0.0f); |
---|
| 583 | ArrayfloatTest(5.00091e-29f); |
---|
[6634] | 584 | |
---|
| 585 | //floatTest(-18.0098f); |
---|
| 586 | //floatTest(-24.07e23f); |
---|
| 587 | //floatTest(-0.0f); |
---|
| 588 | //floatTest(-5.67e-29f); |
---|
[6753] | 589 | |
---|
| 590 | |
---|
[6737] | 591 | //floatTest(-45.7e-32f); |
---|
| 592 | //floatTest(45.7e-33f); |
---|
| 593 | //floatTest(-45.7e-34f); |
---|
| 594 | //floatTest(45.7e-35f); |
---|
[6634] | 595 | } |
---|