How would I read a hex byte, then output it as a letter?

Hey. Well as you can see from my name I'm a beginner.

But here's what I need help doing.

Basically, I have a file. In this file, each hex byte represents a letter. I have a table file, that shows what hex byte represents each letter. So the table file might look like...

1
2
3
4
5
00=A
01=B
02=C
03=D
04=E


And so on...

What I'm trying to do is read a hex byte, then convert to a letter, based on the table file. After that I want to output the letter to a file.

So if I read the hex byte "00", I want to convert that to the letter "A" and then output the letter A to a file. I hope I've explained this well enough.

Anyways, so how would I go about doing this?
It is not clear whether the data is stored in a text file. That is can std::getline be applied to read data from the file?
Well, it's basically stored in a hex file.

In ASCII, certain hex bytes represent letters.

However, sometimes files do not store text using the standard values.

Long story short, I guess I'm asking, how would I convert a hex value to a string value, which can be printed? For instance converting the byte "8B" to the letter "S"?

Alternatively, I could use the getline thing, to grab a line of text from the table and analyze.

For instance, if my table is like this...
1
2
3
00=A
01=B
02=C


I could load "00=A" into a string. But is there a way to convert numbers in a string into an integer?
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
#include <map>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>

int main()
{
    std::map< unsigned char, char > look_up ;

    // populate the map with data from the stream containing the table
    {
        std::istringstream stream( "61=M \n 62=N \n 63=O \n 64=P \n 65=Q \n" ) ;
        std::string byte ;
        char separator ;
        char data ;
        while( stream >> std::setw(2) >> byte >> separator >> data && separator == '=' )
        {
            // look_up[ std::stoi(byte,nullptr,16) ] = data ;

            std::istringstream stm(byte) ;
            int key ;
            stm >> std::hex >> key ;
            look_up[key] = data ;
        }
    }

    // use the map to translate the input data
    {
        std::istringstream stm( "abcdexedcbayaedbc" ) ;
        unsigned char byte ;
        while( stm >> byte )
        {
            auto p = look_up.find(byte) ;
            if( p != look_up.end() ) std::cout << p->second ;
            else std::cout << char(byte) ; // lookup failed
        }
    }
}

http://ideone.com/5pZGBF
Topic archived. No new replies allowed.