39

Need to make int from hex representation string like "0xFA" or better "FA". Need something like atoi("FA"). Is there are any standard solutions for that?

vico
  • 17,051
  • 45
  • 159
  • 315

1 Answers1

83

Try to use strtol():

strtol("FA", NULL, 16);
alk
  • 69,737
  • 10
  • 105
  • 255
V-X
  • 2,979
  • 18
  • 28
  • 1
    Note: it also works with `0xFA`: "(optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is 16 or ​0​)" – Jongware Dec 18 '13 at 09:44
  • 28
    For unsigned, you should use strtoul() to properly handle strings like "0x81234567", otherwise you will get 0xFFFFFFFF returned. – gjcamann May 13 '14 at 15:56