C++ Convert Hex String to Binary String.

Alright, I've searched on google for two hours now, and nothing has worked how I wanted it to (or I'm just not using it right).

I have a std::string that hold a hex value.

std::string sHex = "40";

In my real code the 0x is removed, but it doesn't need to be removed if a method that you know will work will only work with the 0x being there.

So now I need to convert it into a binary string.

Here's the code that I'm using right now:

1
2
3
4
5
6
7
8
9
10
//sRegister holds the 0x40, and this line just puts "40" in sReturn.
string sReturn = sRegister.substr (2, sRegister.length ());
//Now right here I need to convert sReturn from a hex string to a binary string.
sReturn = GetBinaryStringFromHexString (sReturn);
//Create Temp.txt
ofstream oFile ("Temp.txt");
//Output 16 '0's and then sReturn.
oFile << setfill ('0') << setw (16) << sReturn;
//Close the file.
oFile.close ();


If anyone knows how to do this that would be great.

Thanks.
...
the "0x" only lets people know that it is hex. the computer doesnt need the "0x" as long as it knows the value is in hex.
so "40" in hex to the computer is "64" in decimal and "01000000" in binary =P
maybe i misunderstood your question. i dont know its like 3 in the morning here haha

EDIT:: wait.. do you want us to make you functions that turn hex into binary?

EDIT#2:: in that case, wouldnt you use integers and not strings? once again, im tired. sorry
Last edited on
@ edit # 1: Yes

@ edit # 2: Because of the way I'm programming my application I'm using strings. But I can easily use integers if need be.
well converting a hex string to a binary string would be difficult. since its hard to do math with strings haha.
but i think i may have the answer to your question.. hold up
i made this, which converts a decimal number (base 10) to a hexadecimal number (base 16)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdio>
#include <limits>  // needed for pause
using namespace std;

int main()
{
    int value;
    cout << "enter in a number to be converted to hex\n";
    cin >> value;
    char i[50];
    sprintf(i,"%x",value);
    cout << i;
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // pause
}


im trying to make one that converts hex into binary now >_< hold up...
Use stringstream to convert numbers to strings back and forth. This post (http://www.cplusplus.com/forum/beginner/7777/) shows how to do number to string. As exercise do the other functions. Metl wolf's code, although it probably works OK, uses a C function. You should use the "modern", C++ method with stringstream, IMHO.
Converting from hexadecimal to binary is quite easy because every character can be converted independently.
So you just need to replace them.
1
2
3
4
5
6
"0" -> "0000"
"1" -> "0001"
"2" -> "0010"
...
"E" -> "1110"
"F" -> "1111"
was a lot more simple then I thought....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
		string GetBinaryStringFromHexString (string sHex)
		{
			string sReturn = "";
			for (int i = 0; i < sHex.length (); ++i)
			{
				switch (sHex [i])
				{
					case '0': sReturn.append ("0000"); break;
					case '1': sReturn.append ("0001"); break;
					case '2': sReturn.append ("0010"); break;
					case '3': sReturn.append ("0011"); break;
					case '4': sReturn.append ("0100"); break;
					case '5': sReturn.append ("0101"); break;
					case '6': sReturn.append ("0110"); break;
					case '7': sReturn.append ("0111"); break;
					case '8': sReturn.append ("1000"); break;
					case '9': sReturn.append ("1001"); break;
					case 'a': sReturn.append ("1010"); break;
					case 'b': sReturn.append ("1011"); break;
					case 'c': sReturn.append ("1100"); break;
					case 'd': sReturn.append ("1101"); break;
					case 'e': sReturn.append ("1110"); break;
					case 'f': sReturn.append ("1111"); break;
				}
			}
			return sReturn;
		}
Topic archived. No new replies allowed.