具体做法是先将 string 转换为 char* 字符串,再通过相应的类型转换函数转换为想要的数值类型。需要包含标准库函数<stdlib.h>
。
(1)string 转换为 int32_t
string love = "77";
int ilove = atoi(love.c_str());
int ilove = strtol(love.c_str(),NULL,10);
(2)string 转换为 uint32_t
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 转换为 int64_t
string love = "77";
long long llLove=atoll(love.c_str());
(4)string 转换为 uint64_t
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
string love = "77.77";
float fLove = atof(love.c_str());
double dLove = atof(love.c_str());
(6)string 转换为 long double
long double strtold(const char* str, char** endptr);
使用 C++11 引入的 C++ 库函数将 string 转换为数值类型,相应的库函数申明于头文件<string>
中。
名称 | 原型 | 说明 |
---|
stoi | int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to integer (function template ) |
stol | long stol (const string& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to long int (function template) |
stoul | unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to unsigned integer (function template) |
stoll | long long stoll (const string& str, size_t* idx = 0, int base = 10); long long stoll (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to long long (function template) |
stoull | unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10); unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10); | Convert string to unsigned long long (function template) |
stof | float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); | Convert string to float (function template ) |
stod | double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); | Convert string to double (function template ) |
stold | long double stold (const string& str, size_t* idx = 0); long double stold (const wstring& str, size_t* idx = 0); | Convert string to long double (function template) |
形参说明:
str:重载了 string 和 wstring 版本,表示被转换的字符串。
idx:表示一个size_t*
的指针类型,默认为空值。不为空时,转换成功时获取第一个非数值字符相对于首字符的偏移下标,也表示成功转换的字符数量,如 “10” 成功转为数值 10 时,*idx
的值为 2。
base:表示数值的基数,默认是 10 进制。
[1] C++ Reference
[2] strtoul.C++ Reference
[3] strtoull.C++ Reference
[4] strtold.C++ Reference
使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成string。//ostringstream对象用来进行格式化的输出,常用于将各种类型转换为string类型//ostringstream只支持&amp;lt;&amp;lt;操作符template&amp;lt;typename T&amp;gt; string toString(const T&amp;amp; t){ ostringstream oss; //创建一个格式化输出流
用 法: int sscanf(char *string, char *format[,argument,…]);
以上的 format 为 %x 就是将字符串格式化为 16 进制数
#include <stdio>
void main()
char* p = 0x1a;
int nValude = 0;
sscanf(p, %x, &nValude);
使用函数模板将基本数据类型(整型、字符型、实型、布尔型)转换成string。
//ostringstream对象用来进行格式化的输出,常用于将各种类型转换为string类型
//ostringstream只支持<<操作符
template string toString(const T& t){
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsign
记录一些整型转换为string类型的方法~itoa是广泛使用的非标准C语言和C++语言扩展功能。但因为它是一个非标准的C / C++语言功能,因此不能好好的被所有编译器使用。
头文件:< cstdlib >
**返回值:**指向转换后的字符串的指针。
声明: char *itoa (int value, char *str, int base );
//数字 //存储的字符数组或指针 //进制
2.sprintf()
头文件: < cstdio >
**作用:**将附加参数中的值,格式化输出到字符串