字符串转换成数字以及注意事项

来源:互联网 发布:网络舞曲最红 编辑:程序博客网 时间:2024/04/28 12:30

将字符串转换成数字:

有符号转换需要用到atoi,atol,atoll等。无符号转换需要使用strtoul和strtoull等。

(1)常用的转换函数

转换出来的是有符号的:

#include <stdlib.h>

int atoi(cosnt char *nptr);

long atol(const char* nptr);

long long atoll(const  char*nptr);

long long atoq(const  char*nptr);

long int strtol(const char*nptr,const  char**endptr,int base);

long long int strtoll(const char* nptr,char** endptr,int base);

转换出来的是无符号的:               

unsigned long int strtoul(const char*nptr,char** endptr,int base);

unsigned long long int strtoull(const char* nptr,char**endptr,int base);

(2)场景问题

unsigned int  value  = (unsigned int)atoi("3000000000");

printf("value = %u",value);

在64位机器下,value=3000000000。然而在32位机器下value=2147483647

因为atoi函数内部还是使用strtol实现的,则在atoi内部首先是strtol将"3000000000"转换成long类型,

在64位机器下,long是8字节的,最高位是符号位,数据位是63位。

而在32位机器下,long是4字节的,最高字节是符号位,数据位是31位。则在有符号转换函数strtol会将"3000000000"转换时截短为2147483647(有符号数四字节数正数的最大值,即二进制数 0111 1111 1111 1111 1111 1111 1111 1111)。

使用strtoul可正确处理(对于4字节无符号的数有效)。

同理,对于8字节无符号的数,需要使用strtoull,否则即使使用atoll来转换,也会存在同样的问题(无论是64位机器还是32位机器)。

使用代码如下:

Example

12345678910111213141516
/* strtoull example */#include <stdio.h>      /* printf, NULL */#include <stdlib.h>     /* strtoull */int main (){  char szNumbers[] = "250068492 7b06af00 1100011011110101010001100000 0x6fffff";  char * pEnd;  unsigned long long int ulli1, ulli2, ulli3, ulli4;  ulli1 = strtoull (szNumbers, &pEnd, 10);//该数为10进制数  ulli2 = strtoull (pEnd, &pEnd, 16);//该数为16进制数  ulli3 = strtoull (pEnd, &pEnd, 2);//该数为2进制数  ulli4 = strtoull (pEnd, NULL, 0);//该数根据前缀的指定进制数  printf ("The decimal equivalents are: %llu, %llu, %llu and %llu.\n", ulli1, ulli2, ulli3, ulli4);  return 0;}



Output:
The decimal equivalents are: 250068492, 2064035584, 208622688 and 7340031.


 

0 0
原创粉丝点击