/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Wuest co-programmer: ... */ /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput */ #define DEBUG_MODULE_NETWORK /* include your own header */ #include "converter.h" /* using namespace std is default, this needs to be here */ using namespace std; /*! * Standard constructor */ Converter::Converter() { /* set the class id for the base object */ //this->setClassID(CL_ENTITY_MANAGER, "EntityManager"); } /*! * Standard destructor */ Converter::~Converter() { } const int sgnadd = 128; // = 2^7 /*! * Converts an int into a byte-array * @remarks: The int is stored in big-endian * @param x: The int which is to convert * @return: A byte-array that accords the given int value */ byte* Converter::intToByteArray(int x) { const int mod = 256; // = 2^8 byte* result = new byte[INTSIZE]; int sgn; if (x >= 0) sgn = 1; else { sgn = -1; x = -x; } for (int i = 0; i < INTSIZE; i++) { result[i] = x % mod; x /= mod; } if (sgn == -1) result[INTSIZE - 1] += sgnadd; return result; } /*! * Converts a byte-array into an int * @param a: The byte-array which is to convert * @return: An int that accords the given byte-array */ int Converter::byteArrayToInt(byte* a) { int mult = 1; const int step = 256; // = 2 ^ 8 int result = 0; for (int i = 0; i < INTSIZE - 1; i++) { result += a[i] * mult; mult *= step; } printf("tara: %i", result); if (a[INTSIZE - 1] >= sgnadd) { result += (a[INTSIZE - 1] - sgnadd) * mult; result *= -1; } else result += a[INTSIZE - 1] * mult; return result; } /*char* Converter::floatToBinString(float x) { char* result = new char[200]; int pos = 0; int h = (int)x; if (h > x) h--; x -= h; while (h > 0 && pos < 200) { //printf("%i ", pos); if (h % 2 == 1) { result[pos] = '1'; h -= 1; } else result[pos] = '0'; pos++; h /= 2; } //printf("x = %f\n", x); //invert for (int i = 0; i < pos / 2; i++) { char temp = result[i]; result[i] = result[pos - 1 - i]; result[pos - 1 - i] = temp; } result[pos++] = '.'; float sub = 0.5; while (x > 0 && pos < 200) { //printf("%i ", pos); if (x >= sub) { result[pos] = '1'; x -= sub; } else result[pos] = '0'; pos++; sub /= 2; } return result; }*/ char* Converter::floatToBinString(float x) { char* result = new char[200]; int pos = 0; float sub = 1; while (sub < x) sub *= 2; //printf("sub = %f\n", sub); //while (sub >= 1 && pos < 200) while ((x > 0 || sub >= 1) && pos < 200) { if (x >= sub) { result[pos] = '1'; x -= sub; } else result[pos] = '0'; pos++; sub /= 2; if (sub == 0.5f) result[pos++] = '.'; } /*result[pos++] = '.'; sub = 0.5; while (x > 0 && pos < 200) { if (x >= sub) { result[pos] = '1'; x -= sub; } else result[pos] = '0'; pos++; sub /= 2; } */ return result; }