0

How do i convert string like 0x0000 to integer??? This code does not work(Func atof always return 0).

String  str = "0xffff";
(unsigned int)atof(str);
Alex F
  • 165
  • 3
  • 10
  • Hard to say without knowing what `String` is. Of course you might find the [related links](http://stackoverflow.com/questions/7663709/convert-string-to-int-c?rq=1) to the right helpful. – chris May 16 '13 at 06:40

2 Answers2

4

In addition to C++ std::stoi, you can use strtol (or strtoul for unsigned) which works for C and C++:

int i = strtol(str, NULL, 0);

Last parameter 0 means auto-select base to be 8, 10 or 16, depending on how string looks like. For 0x prefix, base 16 would be used. For 0 prefix, base 8 would be used. For other decimals, base 10 is tried.

mvp
  • 111,019
  • 13
  • 122
  • 148
3

Assuming String is std::string, you can use std::stoi:

std::string str("0xffff", 0, 0);
int i = std::stoi(str);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480