Converting from String to Hex C++

Hi,

I have a .txt file that has a list of hex values that i need to load and use. I am able to open the file and read in the hex values as strings and dont know how to convert them into hex. My code looks something like this :

int main () {

int counter = 0;
char str[20][20];

ifstream file_op;

file_op.open("gcc_trace.txt");
file_op >> str[0];

while(!file_op.eof() ){

cout << str[counter] << endl;
counter = counter + 1;
file_op >> str[counter];

}

file_op.close();
return 0;
}

I need to convert the values stored in var str into hex.
You can use std::hex manipulator to read hex values from stream:
1
2
3
4
5
6
7
8
9
10
#include <fstream>
#include <iostream>
int main()
{
        std::ifstream f("./hex");
        int a;
        f>>std::hex>>a;
        std::cout<<std::hex<<a;
        return 0;
}

here's about manipulators: http://www.cplusplus.com/reference/iostream/manipulators/
Thank you...that worked wonderfully :)
Topic archived. No new replies allowed.