Changeset 6341 in orxonox.OLD for trunk/src/lib/network/converter.cc
- Timestamp:
- Dec 30, 2005, 1:57:12 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/converter.cc
r6139 r6341 54 54 { 55 55 const int mod = 256; // = 2^8 56 57 56 57 58 58 byte* result = new byte[INTSIZE]; 59 59 int sgn; … … 65 65 x = -x; 66 66 } 67 67 68 68 for (int i = 0; i < INTSIZE; i++) 69 69 { … … 71 71 x /= mod; 72 72 } 73 73 74 74 if (sgn == -1) 75 75 result[INTSIZE - 1] += sgnadd; 76 77 78 return result; 76 77 78 return result; 79 } 80 81 /*! 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 */ 87 int 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; 79 116 } 80 117 … … 84 121 * @return: An int that accords the given byte-array 85 122 */ 86 int Converter::byteArrayToInt( byte* a)123 int Converter::byteArrayToInt(const byte* a, int* x) 87 124 { 88 125 int mult = 1; 89 126 const int step = 256; // = 2 ^ 8 90 int result= 0;127 *x = 0; 91 128 for (int i = 0; i < INTSIZE - 1; i++) 92 129 { 93 result+= a[i] * mult;130 *x += a[i] * mult; 94 131 mult *= step; 95 132 } 96 97 printf("tara: %i", result); 98 133 99 134 if (a[INTSIZE - 1] >= sgnadd) 100 135 { 101 result += (a[INTSIZE - 1] - sgnadd) * mult; 102 result *= -1; 103 } 104 else 105 result += a[INTSIZE - 1] * mult; 106 107 return result; 108 } 109 110 /*char* Converter::floatToBinString(float x) 136 *x += (a[INTSIZE - 1] - sgnadd) * mult; 137 *x *= -1; 138 } 139 else 140 *x += a[INTSIZE - 1] * mult; 141 142 return INTSIZE; 143 } 144 145 /*! 146 * Converts a float into a string containing its binary representation 147 */ 148 char* Converter::floatToBinString(float x) 111 149 { 112 150 char* result = new char[200]; 113 151 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 152 153 if (x < 0) 154 { 155 result[pos++] = '-'; 156 x = -x; 157 } 158 159 float sub = 1; 160 while (sub < x) 161 sub *= 2; 162 163 while ((x > 0 || sub >= 1) && pos < 200) 164 { 154 165 if (x >= sub) 155 166 { … … 161 172 pos++; 162 173 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; 173 174 175 if (sub == 0.5f) 176 result[pos++] = '.'; 177 } 178 179 return result; 180 } 181 182 const int expmult = 8388608; //2^23 183 184 /*! 185 * Converts a float value into a byte-array 186 * @param x: The float which is to convert 187 * @return: A byte-array which accords the given float 188 */ 189 byte* Converter::floatToByteArray(float x) 190 { 191 int mantisse = 0; 192 int exponent = 128; 193 194 int sgn; 195 if (x < 0) 196 { 197 x = -x; 198 sgn = -1; 199 } 200 else 201 sgn = 1; 202 174 203 float sub = 1; 175 204 while (sub < x) 205 { 176 206 sub *= 2; 177 178 //printf("sub = %f\n", sub); 179 180 //while (sub >= 1 && pos < 200) 181 while ((x > 0 || sub >= 1) && pos < 200) 207 exponent++; 208 } 209 210 while (x > 0) 182 211 { 183 212 if (x >= sub) 184 213 { 185 result[pos] = '1';214 mantisse += 1; 186 215 x -= sub; 187 216 } 188 else 189 result[pos] = '0';190 pos++;217 218 mantisse *= 2; 219 exponent--; 191 220 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) 221 } 222 exponent++; 223 mantisse /= 2; 224 while (mantisse < expmult) 225 { 226 mantisse *= 2; 227 exponent--; 228 } 229 230 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 231 232 mantisse -= expmult; 233 234 int hx = mantisse + expmult * exponent; 235 byte* result = intToByteArray(hx); 236 if (sgn == -1) 237 result[3] += sgnadd; 238 239 return result; 240 } 241 242 243 /*! 244 * Converts a byte-array into a float value 245 * @param a: The byte-array which is to convert 246 * @return: A float value which accords the given byte-array 247 */ 248 float Converter::byteArrayToFloat(byte* a) 249 { 250 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 251 int mantisse = hmant % expmult; 252 mantisse += expmult; 253 254 int hexp = a[2] + a[3] * 256; 255 int exponent = (hexp / 128) % 256 - 128; 256 257 int sgn; 258 if (a[3] >= sgnadd) 259 sgn = -1; 260 else 261 sgn = 1; 262 263 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 264 265 float emult = 1; 266 if (exponent > 0) 267 for (int i = 0; i < exponent; i++) 268 emult *= 2; 269 else if (exponent < 0) 270 for (int i = 0; i > exponent; i--) 271 emult /= 2; 272 273 float result = mantisse * emult; 274 if (sgn == -1) 275 result = -result; 276 277 return result; 278 } 279 280 /*! 281 * Converts a float value into a byte-array 282 * @param x: The float which is to convert 283 * @return: A byte-array which accords the given float 284 */ 285 byte* Converter::_floatToByteArray(float x) 286 { 287 byte* p = (byte*)&x; 288 byte* result = new byte[4]; 289 for (int i = 0; i < 4; i++) 290 result[i] = p[i]; 291 return result; 292 } 293 294 295 /*! 296 * Converts a byte-array into a float value 297 * @param a: The byte-array which is to convert 298 * @return: A float value which accords the given byte-array 299 */ 300 float Converter::_byteArrayToFloat(byte* a) 301 { 302 float* p = (float*)a; 303 float result = *p; 304 return result; 305 } 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 */ 312 int 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 320 //handle 0 else function will loop for ever 321 if ( x == 0 ) 322 { 323 for ( int i = 0; i<FLOATSIZE; i++) 324 a[i] = 0; 325 return FLOATSIZE; 326 } 327 328 int mantisse = 0; 329 int exponent = 128; 330 331 int sgn; 332 if (x < 0) 333 { 334 x = -x; 335 sgn = -1; 336 } 337 else 338 sgn = 1; 339 340 float sub = 1; 341 while (sub < x) 342 { 343 sub *= 2; 344 exponent++; 345 } 346 347 while (x > 0) 200 348 { 201 349 if (x >= sub) 202 350 { 203 result[pos] = '1';351 mantisse += 1; 204 352 x -= sub; 205 353 } 206 else 207 result[pos] = '0';208 pos++;354 355 mantisse *= 2; 356 exponent--; 209 357 sub /= 2; 210 358 } 211 */ 212 213 return result; 214 } 359 exponent++; 360 mantisse /= 2; 361 while (mantisse < expmult) 362 { 363 mantisse *= 2; 364 exponent--; 365 } 366 367 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 368 369 mantisse -= expmult; 370 371 int hx = mantisse + expmult * exponent; 372 int result = intToByteArray(hx, a, length); 373 if (sgn == -1) 374 a[3] += sgnadd; 375 376 return result; 377 } 378 379 380 /*! 381 * Converts a byte-array into a float value 382 * @param a: The byte-array which is to convert 383 * @return: A float value which accords the given byte-array 384 */ 385 int Converter::byteArrayToFloat(const byte* a, float* x) 386 { 387 //handle 0 388 for (int i = 0; i<FLOATSIZE; i++) 389 { 390 if (a[i]!=0) 391 break; 392 if ( i==FLOATSIZE-1 ) 393 { 394 *x = 0.0f; 395 return FLOATSIZE; 396 } 397 } 398 399 400 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 401 int mantisse = hmant % expmult; 402 mantisse += expmult; 403 404 int hexp = a[2] + a[3] * 256; 405 int exponent = (hexp / 128) % 256 - 128; 406 407 int sgn; 408 if (a[3] >= sgnadd) 409 sgn = -1; 410 else 411 sgn = 1; 412 413 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 414 415 float emult = 1; 416 if (exponent > 0) 417 for (int i = 0; i < exponent; i++) 418 emult *= 2; 419 else if (exponent < 0) 420 for (int i = 0; i > exponent; i--) 421 emult /= 2; 422 423 *x = mantisse * emult; 424 if (sgn == -1) 425 *x = - *x; 426 427 return FLOATSIZE; 428 } 429 430 /** 431 * copies a strint to a byte array 432 * @param s: string to copy 433 * @param a: byte array 434 * @param length: string length 435 * @return: the used number of bytes in byte array 436 */ 437 int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength ) 438 { 439 if ( length+INTSIZE > maxLength ) 440 { 441 PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE); 442 return -1; 443 } 444 445 int n = Converter::intToByteArray( length, a, maxLength ); 446 447 memcpy( a+INTSIZE, s, length ); 448 449 return length + INTSIZE; 450 } 451 452 /** 453 * reads a string out of a byte array 454 * @param a: byte array 455 * @param s: string 456 * @param maxLength: max bytes to copy 457 * @return: the number of read bytes in byte array 458 */ 459 int Converter::byteArrayToString( const byte * a, char * s, int maxLength ) 460 { 461 int length; 462 463 int n = Converter::byteArrayToInt( a, &length ); 464 465 466 if ( length+1 > maxLength ) 467 { 468 PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 ); 469 strncpy(s,"",maxLength); 470 return -1; 471 } 472 473 memcpy( s, a+n, length ); 474 s[length] = '\0'; 475 476 return n+length; 477 } 478 479 /** 480 * reads a string out of a byte array and allocates memory for string 481 * @param a: byte array 482 * @param s: string 483 * @param maxLength: max bytes to copy 484 * @return: the number of read bytes in byte array 485 */ 486 int Converter::byteArrayToStringM( const byte * a, char*& s ) 487 { 488 int length; 489 490 int n = Converter::byteArrayToInt( a, &length ); 491 492 s = new char[length+1]; 493 494 if ( !s ) 495 { 496 PRINTF(1)("Could not allocate memory!\n" ); 497 return -1; 498 } 499 500 memcpy( s, a+n, length ); 501 s[length] = '\0'; 502 503 return n+length; 504 } 505
Note: See TracChangeset
for help on using the changeset viewer.