-2

Possible Duplicate:
C++ convert hex string to signed integer

I have the string line which is a hexadecimal number say like 12ab43c..(but I have read it as a string) and I would like to pass it to an unsigned char* linehex or directly to a hexadecimal so I can later use it in my program for further computations. Which is the most efficient way to do this?

Community
  • 1
  • 1
Hashed
  • 57
  • 1
  • 2
  • 6

1 Answers1

3

The easiest is probably to read it as a number to start with, instead of reading it as a string, then converting. For example:

some_stream >> std::hex >> your_number;

Quick demo code:

#include <iostream>

int main() {    
    int x;

    std::cin >> std::hex >> x;

    std::cout << x << "\n";
    return 0;
}

Input: ff
Output: 255

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111