tags:

views:

107

answers:

2

i have a unicode mapping stored in a file.

like this line below with tab delimited.

a   0B85    0 0B85

second column is a unicode character. i want to convert that to 0x0B85 which is to be stored in int variable.

how to do it?

+6  A: 

You could use strtol, which can parse numbers into longs, which you can then assign to your int. strtol can parse numbers with any radix from 2 to 36 (i.e. any radix that can be represented with alphanumeric charaters).

For example:

#include <cstdlib>
using namespace std;

char *token;
...
// assign data from your file to token
...

char *err;   // points to location of error, or final '\0' if no error.
int x = strtol(token, &err, 16);   // convert hex string to int
tgamblin
While it doesn't matter for this answer, strtol is limited to bases 2 - 36.
R Samuel Klatchko
tgamblin,*token doesnt accept string. bcoz 0B8A seems to be string.
coder
@coder: You're going to have to try a *little* harder. Look at the documentation for string. You can get a const char* with the c_str() method.
tgamblin
+8  A: 

You've asked for C++, so here is the canonical C++ solution using streams:

#include <iostream>

int main()
{
    int p;
    std::cin >> std::hex >> p;
    std::cout << "Got " << p << std::endl;
    return 0;
}

You can substitute std::cin for a string-stream if that's required in your case.

Eli Bendersky
that didnt solve my issue
coder
@coder: care to elaborate? This is a building block that can be adapted to your particular need. What did you expect to see?
Eli Bendersky