类型转换有关问题,大家过来看
类型转换问题,大家过来看
我在文件中写了一个16进制数0x00000001
我读入了以后变成 "0x00000001 "
请问在C语言中怎么把它变成1?
------解决方案--------------------
char str[]= "0x00000001 ";
int i;
sscanf(str, "%x ", &i);
------解决方案--------------------
当然楼主还可以这个函数strtol
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char buf[]= "0x00000001 ";
int i;
i=(int)strtol(buf,NULL,16);
printf( "i=%d\n ",i);
return 0;
}
------解决方案--------------------
#include <stdio.h>
#include <string.h>
int main()
{
char *p= "0x00000001 ";
int n = *(p+strlen(p)-1)- '0 ';
printf( "%d ",n);
return 0;
}
可以这样
我在文件中写了一个16进制数0x00000001
我读入了以后变成 "0x00000001 "
请问在C语言中怎么把它变成1?
------解决方案--------------------
char str[]= "0x00000001 ";
int i;
sscanf(str, "%x ", &i);
------解决方案--------------------
当然楼主还可以这个函数strtol
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char buf[]= "0x00000001 ";
int i;
i=(int)strtol(buf,NULL,16);
printf( "i=%d\n ",i);
return 0;
}
------解决方案--------------------
#include <stdio.h>
#include <string.h>
int main()
{
char *p= "0x00000001 ";
int n = *(p+strlen(p)-1)- '0 ';
printf( "%d ",n);
return 0;
}
可以这样