Convert Binary String to Hex String

Ok, I've searched on google for a few hours now, and nothing is working how I want it to.

I have this binary string:

std::string sBinary = "00000000000000000000000000000000";

And I have functions that edit it depending on some user input.

Now I need a function that I can call and pass in sBinary and have it return a 8 character hex string so I can use it in File I/O like this:

1
2
3
ofstream oFile ("Result.txt");
oFile << "0x" << tcaInput << " 0x" << GetHexFromBin (sBinary);
oFile.close ();


Can anyone write/help_me_write a function that can do this?
At first you must convert from binary to decimal number ,then convert that to hex with setbase() function.
Hope this helps you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include <iomanip>
#include<math.h>
#include<string>
using namespace std;
int main()
{
long int longint=0;
string buf;
cin>>buf;
int len=buf.size();
for(int i=0;i<len;i++)
{
longint+=( buf[len-i-1]-48) * pow(2,i);


}
cout<<setbase(16);
cout<<longint;



return 0;
}

test this code...
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
string GetHexFromBin(string sBinary)
{
	string rest("0x"),tmp,chr = "0000";
	int len = sBinary.length()/4;
	chr = chr.substr(0,len);
	sBinary = chr+sBinary;
	for (int i=0;i<sBinary.length();i+=4)
	{
		tmp = sBinary.substr(i,4);
		if (!tmp.compare("0000"))
		{
			rest = rest + "0";
		}
		else if (!tmp.compare("0001"))
		{
			rest = rest + "1";
		}
		else if (!tmp.compare("0010"))
		{
			rest = rest + "2";
		}
		else if (!tmp.compare("0011"))
		{
			rest = rest + "3";
		}
		else if (!tmp.compare("0100"))
		{
			rest = rest + "4";
		}
		else if (!tmp.compare("0101"))
		{
			rest = rest + "5";
		}
		else if (!tmp.compare("0110"))
		{
			rest = rest + "6";
		}
		else if (!tmp.compare("0111"))
		{
			rest = rest + "7";
		}
		else if (!tmp.compare("1000"))
		{
			rest = rest + "8";
		}
		else if (!tmp.compare("1001"))
		{
			rest = rest + "9";
		}
		else if (!tmp.compare("1010"))
		{
			rest = rest + "A";
		}
		else if (!tmp.compare("1011"))
		{
			rest = rest + "B";
		}
		else if (!tmp.compare("1100"))
		{
			rest = rest + "C";
		}
		else if (!tmp.compare("1101"))
		{
			rest = rest + "D";
		}
		else if (!tmp.compare("1110"))
		{
			rest = rest + "E";
		}
		else if (!tmp.compare("1111"))
		{
			rest = rest + "F";
		}
		else
		{
			continue;
		}
	}
	return rest;
}
Another way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    string bin("00000001000000111111001100001000");
    int result =0 ;

    for(size_t count = 0; count < bin.length() ; ++count)
    {
        result *=2;
        result += bin[count]=='1'? 1 :0;
    }  

    stringstream ss;
    ss << "0x" << hex << setw(8) << setfill('0')  << result;

    string hexVal(ss.str());
    
Thanks guys, will test them when I can.
Topic archived. No new replies allowed.