Printing an ASCII character from a hex value

I have a string that holds a series of hex numbers. Each pair within this string translates to an ascii character. I need to print out the ascii translation of this string. Let's just focus on the first pair.

Say that my string is string s = "4d";

A quick lookup on an ascii table shows that this translates to ascii character "M". I cannot figure out how to get "4d" to print out as "M". I'm sure that this is a very common issue, and I have tried quite a few different ideas from various searches, but I'm just not getting this to come out. Can anybody tell me how to do this?

Here is one thing that I tried:

1
2
string s = "4D";
cout << "ASCII: " << hex << s << endl;


Do I need to convert the string somehow? Very confused.
Thanks!
You need to "parse" the string. (Searching with that word might help you out.)
Thanks, but if I understand what you are suggesting, that's not my issue. I think that what you are suggesting is that I need to separate the '4' from the 'D'. If so, I can do that easily. However, separately, these characters mean nothing to me. Only when combined into the pair '4D' do they have a lookup value in the ascii table. What I need to know is how to bring back that lookup result (e.g. "M").

Am I missing your point? If not, can anybody suggest how I get this ascii value? Thanks!

edit: Also, I know that this translates into 01001101 in binary, but I don't know how to get from the string to the binary value. Literally, decimal 4 translates to "End of Transmission" and decimal 13 (hex D) translates to a carriage return. I need to figure out how to get this interpreted as a single hexadecimal value. Thanks.
Last edited on
I'm sure that this is a very common issue


Is this for class? If not, I think it's safe to say that this never happens. How was this file made?

You're string does not hold decimal values (well, not in appearance). A string "4D" is the same as a char array {'4', 'D', '\0'}. The point is that "4D" is two bytes, and you want it in one byte. Not only that, but the bytes are in ASCII, and you don't want that. "4D" is not 01001101 (0x4D), it is "00100010, 00101100" (0x34, 0x44).

Anyway, you need to do some math and make '4' equal 4 (0x04), and 'D' equal 13 (0x0D). Multiply 4 by 16 and add it to 13 and end up with 0x4D, your character 'M'.

If this is your attempt to read a binary file, let us know because there is a much more straight forward way.
Last edited on
This is for a self study thing that I'm doing in cryptography. Here's how I solved it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char convertHexPair(string hex) {
    /* note: hex will be a two character string */
    /* the first char will always be between 4 and 7 */
    int sixteensDig = (hex[0] - 48) * 16;
    int onesDig = hex[1];

    /* convert to upper case */
    if ( (onesDig >= 97) && (onesDig <= 102) )
        onesDig -= 32;

    if (onesDig < 65) {
        /* numeric value for this digit - convert from ascii */
        return char(sixteensDig + onesDig - 48);
    } else {
        /* A-F in this digit - convert from ascii */
        return char(sixteensDig + onesDig - 55);
    }
}
Last edited on
These are called Magic Numbers, and they are bad :)

I know you know what they stand for, but better code is to write the actual character rather than the number. For example, line 11: if (onesDig < 'A'). As for the others, I haven't memorized the ASCII table, so I don't really know what your code is doing.

Edit: okay, uppercase is saying:
1
2
if ( (onesDig >= 'a') && (onesDig <= 'f') )
        onesDig -= ('a' - 'A');


Easier for other people. Easier for you in the long run I think.
Last edited on
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

1
2
3
4
5
// TODO: handle the error case where a string is passed in that results in a value above 255, or non-numeric string
char convertHexPair(string hex) {
  long lNum = strtol(hex.c_str());
  return (char)lNum;
}


Last edited on
http://cplusplus.com/reference/iostream/manipulators/hex/
I've never used it for reading data, but this might just be what you're looking for.
Topic archived. No new replies allowed.