Number System Conversions in C++

A posting which I will probably update from time to time that summarizes the conversion functions I encounter in C++. Hope others will find this useful too. 32-bit IEEE 754 floating point value to binary string [code language="cpp"] std::string GetBinary32( float value ) { union { float input; // assumes sizeof(float) == sizeof(int) int output; } data; data.input = value; std::bitset<sizeof(float) * CHAR_BIT> bits(data.output); std::string mystring = bits.to_string<char, std::char_traits<char>, std::allocator<char> >(); return mystring; } [/code] 32-bit IEEE 754 binary string to floating point value [code language="cpp"] float GetFloat32( std::string Binary ) { int HexNumber = Binary2Hex( Binary ); bool negative = !!(HexNumber & 0x80000000); int exponent = (HexNumber & 0x7f800000) >> 23; int sign = negative ? -1 : 1; // Subtract 127 from the exponent exponent -= 127; // Convert the mantissa into decimal using the // last 23 bits int power = -1; float total = 0.0; for ( int i = 0; i < 23; i++ ) { int c = Binary[ i + 9 ] - '0'; total += (float) c * (float) pow( 2.0, power ); power--; } total += 1.0; float value = sign * (float) pow( 2.0, exponent ) * total; return value; } [/code] Binary string to hexadecimal value [code language="cpp"] int Bin2Hex( std::string Binary ) { std::bitset<64> set(Binary); int hex = set.to_ulong(); return hex; } [/code] Binary string to hexadecimal string [code language="cpp"] #include <sstream> std::string Binary2HexString( std::string Binary ) { std::bitset<64> set(Binary); int hex = set.to_ulong(); std::stringstream hex_str; hex_str << std::hex << std::uppercase << hex; return hex_str.str(); } [/code] Integer value to Gray code [code language="cpp"] std::string Dec2Gray( unsigned n ) { n = (n>>1) ^ n; const size_t size = sizeof(n) * 8; char result[size]; unsigned index = size; do { result[--index] = '0' + (n & 1); } while (n >>= 1); return std::string(result + index, result + size); } [/code] Gray string to binary string [code language="cpp"] std::string Gray2Bin( std::string gray ) { int len = gray.length(); std::string bin = gray; for ( int i = len - 2; i >= 0; i-- ) { int bk = bin.at( i + 1 ) - '0'; int gi = gray.at( i + 1 ) - '0'; if ( bk == 0 ) { bin[ i ] = gi == 1 ? '1' : '0'; } else { bin[ i ] = gi == 1 ? '0' : '1'; } } return bin; } [/code] Binary string to integer value [code language="cpp"] long Bin2Dec( std::string bin ) { char * ptr; long parsed = strtol( bin.c_str(), & ptr, 2 ); return parsed; } [/code] Integer value to binary string [code language="cpp"] std::string Dec2Bin( unsigned n ) { const size_t size = sizeof(n) * 8; char result[size]; unsigned index = size; do { result[--index] = '0' + (n & 1); } while (n >>= 1); return std::string(result + index, result + size); } [/code] Integer string to integer value [code language="cpp"] int IntStr2Int( std::string intstr ) { int value = atoi( intstr.c_str() ); return value; } [/code] Integer array to integer string [code language="cpp"] std::string IntArray2String( int int_array[], int size ) { std::stringstream ss; for (int i = 0; i < size; i++) { ss << int_array[i]; } return ss.str(); } [/code] Integer to integer array [code language="cpp"] int* Int2IntArray( unsigned value ) { // Number of digits int size = value % 10; int val = size; int *arr = new int[ size ]; int i = size - 1; while( i >= 0 ) { arr[ i ] = val; value /= 10; val = value % 10; i--; } return arr; } [/code] Decimal to any base [code language="cpp"] void dec2base(int num, int base, std::stringstream& ss ) { if (num >= base) { dec2base(num/base, base, ss ); } ss << num % base; } [/code] Double to binary32 [code language="cpp"] int DoubleToBinary32(double value) { int minus = 0, integer, exponent = 127, fraction = 0, i, result; if(value < 0) { minus = 0x80000000; value = -value; } integer = floor(value); value -= integer; for(i = 22; i >= 0; i--) { value += value; fraction += floor(value) * pow(2, i); value -= floor(value); } while((integer != 1) && (exponent > 0) && (exponent < 255)) { if(integer > 1) { fraction = (integer&1)<<22 + fraction>>1; integer = integer>>1; exponent++; } else { integer = (fraction&0x400000)>>22; fraction = (fraction&0x3FFFFF)<<1; value += value; fraction += floor(value); value -= floor(value); exponent--; } } result = minus + exponent<<23 + fraction; return(result); } [/code] Binary32 to double [code language="cpp"] double Binary32ToDouble(int value) { int minus = -1, exponent; double fraction, result; if(value&0x80000000 == 0) minus = 1; exponent = ((value&0x7F800000)>>23) - 127; fraction = value&0x7FFFFF + 0x800000; fraction = fraction / 0x800000; result = minus * fraction * pow(2, exponent); return(result); } [/code] std::bitset to System::String^ [code language="cpp"] // Compiled as a CLR empty project in Visual Studio #include <bitset> #include <iostream> #include <string> using namespace System; const unsigned int number_elements = 5; template<unsigned int N> System::String^ GetBitsetString( std::bitset< N > bset ) { std::string str = bset.template to_string<char, std::char_traits<char>, std::allocator<char> >(); return gcnew String(str.c_str()); } int main( ) { std::bitset<number_elements> bits( std::string( "10101" ) ); String^ str2 = GetBitsetString( bits ); Console::WriteLine(str2); } [/code]

Comments

Popular posts from this blog

Using the Supervisor Controller Pattern to access View controls in MVVM

Getting started with Tesseract optical character recognition (OCR) library in Visual Studio

How to send an e-mail via Google SMTP using C#