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

将字符串转换成数字:

有符号转换须要用到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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* 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.
", ulli1, ulli2, ulli3, ulli4);
  return 0;
}


Output:

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


 

原文地址:https://www.cnblogs.com/liguangsunls/p/7063452.html