# 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

```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;
}
```

32-bit IEEE 754 binary string to floating point value

```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;
}
```

```int Bin2Hex( std::string Binary )
{
std::bitset<64> set(Binary);
int hex = set.to_ulong();
return hex;
}
```

```#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();
}
```

Integer value to Gray code

```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);
}
```

Gray string to binary string

```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;
}
```

Binary string to integer value

```long Bin2Dec( std::string bin )
{
char * ptr;
long parsed = strtol( bin.c_str(), & ptr, 2 );
return parsed;
}
```

Integer value to binary string

```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);
}
```

Integer string to integer value

```int IntStr2Int( std::string intstr )
{
int value = atoi( intstr.c_str() );
return value;
}
```

Integer array to integer string

```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();
}
```

Integer to integer array

```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;
}
```

Decimal to any base

```void dec2base(int num, int base, std::stringstream& ss )
{
if (num >= base)
{
dec2base(num/base, base, ss );
}

ss << num % base;
}
```

Double to binary32

```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);
}
```

Binary32 to double

```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);
}
```

std::bitset to System::String^

```// 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);
}
```