-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?

4

1 回答 1

3

最简单的可能是将其作为数字读取,而不是将其作为字符串读取,然后进行转换。例如:

some_stream >> std::hex >> your_number;

快速演示代码:

#include <iostream>

int main() {    
    int x;

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

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

输入:ff
输出:255

于 2012-09-24T15:42:27.857 回答