The first function converts Hex to ASCII
the second function converts ASCII to Binary
I think I already got the first function to work. But when I try to compare the output of my program with some output from a website that I am using, they don't match.
Okay, I understand now. What's your expected output with c10 as the argument? It should be a garbage string as not all characters are printable. My output is:
a lookup table of strings for 0-15 for binary is the way to get binary text in and out. Binary and hex have a relationship: each hex digit is 4 binary digits.
0 = 0000 = 0x00
1 = 0001 = 0x01
2 = 0010 = 0x02
3 = 0011 = 0x03
...
15 = 1111 = 0x0F
you can also do conversions to and from binary with the shift operator (>>).
13 >> 1 shifts the bits of 13 one place. Modulo 2 and stuff can peel it apart.
You can also use base 2 and logs. if you didn't know log is base 10, lg is base 2, ln is base e. But I don't think c++ has lg, you have to do change of base trick.
I am assuming you want binary text here (0B11010101100 format stuff)
not binary in the usual sense of the term (computer formatted numbers, which simply contain the value of the data). You can't meaningfully print computer formatted data, you want to convert back to hex, base10, text, or something useful.
sscanf can reverse hex into a number. But for these huge values, you need a big-int class that supports it or roll your own.
Consider how to do this by hand first. Take the 1st 4 characters in your hex string: 315c. This represents 2 ASCII characters.
The first string is represented by "31". This means that the ASCII value is 3*16+1. So to convert it the ASCII string "31" to the right ASCII value, you must:
- convert ASCII '3' to the number 3.
- convert ASCII '1' to the number 1.
- compute 3*16+1.
For the second pair of numbers, you have to do something similar:
- Convert ASCII '5' to the number 5.
- Convert ASCII 'c' to the number 12.
- Compute 5*16+12