c++ 字符串与数字的转换(转)

 字符串转数字

 
#include<iostream>
#include<string>
#include <stdio.h>
 
using namespace std;
 
int main()
{
	char str[]= "123456"; 
	int a; 
	sscanf(str, "%d", &a); //换成%x等可以实现进制之间的转换
	
	cout << a;
	
	return 0;
}

数字转字符串

//字符串转数字 
#include<iostream>
#include<string>
#include <stdio.h>
 
using namespace std;
 
int main()
{
	char str[]= "123.456"; 
	double a; 
	sscanf(str, "%lf", &a); //换成%X之类可以实现进制之间的转换
	
	cout << a;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/BODOA/article/details/82561445
今日推荐