数据转换
数据之间的转换
字符数组转:
- int : atoi (char * ch)
- long : atol (char * ch)
- float : atof (char * ch)
字符数组转:
float : strtof (char * ch, NULL)
double : strtod(char * ch, NULL)
long: strtol(char * ch, NULL, int base)
unsigned long: strtoul(char * ch, NULL, int base)
base >= 2 && base <= 36
3、4将字符串视为base进制转为十进制,堪比java进制转换
Code
#include <cstdio>
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
cout << atoi(s.c_str()) << '\n';
cout << atol(s.c_str()) << '\n';
cout << atof(s.c_str()) << '\n';
cout << strtof(s.c_str(), NULL) << '\n';
cout << strtod(s.c_str(), NULL) << '\n';
///top : 2147483647 = 7fffffff
cout << strtol(s.c_str(), NULL, 16) << '\n';
///top : 4294967295 = ffffffff
cout << strtoul(s.c_str(), NULL, 16) << '\n';
///char [] -> string : copy
char a[] = "123456";
string b(a);
cout << b << '\n';
///string -> char [] : ch.c_str()
string ch = "987654";
cout << ch.c_str() << '\n';
}
string转char * s:
char c[20];
string s = "1234";
strcpy(c, s.c_str());
暂存一段cin关闭同步
#define quick_cin ios::sync_with_stdio(false);