开发者

Character [hex] to integer

开发者 https://www.devze.com 2023-02-01 17:57 出处:网络
I tried using atoi function to do this, but it didn\'t work. I want to convert a string that says \"0x44\" to an integer (as 0x44 or its decimal equivalent).

I tried using atoi function to do this, but it didn't work.

I want to convert a string that says "0x44" to an integer (as 0x44 or its decimal equivalent).

Does anyone know of an开发者_Go百科y functions that could do that?


Yes. Use strtol instead, and specify 0 as the base.


I think this is what you are looking for...

#include<stdio.h>
#include<stdlib.h>

void main() {
  char* hex = "0x44";
  int i;

  sscanf( hex, "%x", &i );
  printf("%d\n", i);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消