Converting strings to binary or hexadecimal

I have a string I get from a text file and want to convert it to binary. Is there any method to achieve this that you know off? I am very stuck right now. Thank you for your help.
If to assume that a byte has eight bits then the code could look for example the following way


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char s[] = "ABC";
char t[ 2 * sizeof( s ) - 1];

char *q = t;

for ( char *p = s; *p; ++p, ++q )
{
	*q = ( *p & 0xF0 ) >> 4;
	*q += ( *q > 9 ) ? 'A' - 10 : '0';
	*++q = *p & 0x0F;
	*q += ( *q > 9 ) ? 'A' - 10 : '0';
}

*q = '\0';

std::cout << "s = \"" << s << "\"\n";
std::cout << "t = \"" << t << "\"\n";
Topic archived. No new replies allowed.