C语言数据类型装换

使用C标准库函数

具体做法是先将string转换为char*字符串,再通过相应的类型转换函数转换为想要的数值类型。需要包含标准库函数<stdlib.h>。 
(1)string转换为int32_t

1

2

3

4

5

string love="77";

int ilove=atoi(love.c_str());

//或者16位平台转换为long int

int ilove=strtol(love.c_str(),NULL,10);

  (2)string转换为uint32_t

1

2

3

4

5

6

7

8

9

//str:待转换字符串

//endptr:指向str中数字后第一个非数字字符

//base:转换基数(进制),范围从2至36

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

#示例

string love="77";

unsigned long ul;

ul = strtoul(love.c_str(), NULL, 10);

  (3)string转换为uint64_t

1

2

string love="77";

long long llLove=atoll(love.c_str());

  (4)string转换为uint64_t

1

2

3

4

5

6

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

#示例

string love="77";

unsigned long long ull;

ull = strtoull (love.c_str(), NULL, 0);

  (5)string转换为float或double

1

2

3

string love="77.77";

float fLove=atof(love.c_str());

double dLove=atof(love.c_str());

  (6)string转换为long double

1

long double strtold (const char* str, char** endptr);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值