身为一个学计算机的。。随手换个进制问题倒是不大,但最近发现了一些函数挺不错的。
进制这个概念是针对于人的,在机器中都是一样的表达方式,所以一般涉及进制转换问题的都是将一个数以某种进制的形式转换为字符串或打印结果。
- printf() / cout(8,10,16进制间相互转换)
二进制的话c++还可以用bitset来转换
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 0x1F;
cout << dec << n << endl; //默认10进制,dec可省略
cout << hex << n << endl; //16进制(小写)
cout << oct << n << endl; //8进制
cout << bitset<32>(n) << endl; //2进制
printf("%d\n", n); //10进制
printf("%x\n", n); //16进制(小写),%X为大写
printf("%o\n", n); //8进制
return 0;
}
- sprintf() / sscanf()(8,10,16进制间相互转换)
sscanf()的功能是从string里读出内容,sprintf()的功能是往string里写入内容。
#include<bits/stdc++.h>
using namespace std;
int main()
{
char *str = "12345";
int dec;
sscanf(str, "%d", &dec);
cout << dec << endl; //12345
int hex;
sscanf(str, "%X", &hex);
cout << hex << endl; //74565 ( = 0x12345 )
int oct;
sscanf(str, "%o", &oct);
cout << oct << endl; //5349 ( = 012345 )
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
char dec[20], hex[20], oct[20];
int n = 100;
sprintf(dec, "%d", n);
sprintf(hex, "%X", n);
sprintf(oct, "%o", n);
puts(dec); //100
puts(hex); //64
puts(oct); //144
return 0;
}
- strtol()(将一个任意进制字符串转换为10进制整数)
long int strtol(const char *nptr, char **endptr, int base)
参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如’0x’前置字符则会使用16进制做转换、遇到’0’前置字符而不是’0x’的时候会使用8进制做转换。
一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时(‘\0’)结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回;若参数endptr为NULL,则会不返回非法字符串。
使用示例:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char *stop;
cout << strtol("Ff", &stop, 16) << endl; //255
cout << strtol("0xFf", &stop, 16) << endl; //255
cout << strtol("027", &stop, 8) << endl; //23
cout << strtol("27", &stop, 8) << endl; //23
cout << strtol("zzf", &stop, 36) << endl; //46635
cout << strtol("100", &stop, 0) << endl; //100
cout << strtol("100", &stop, 2) << endl; //4
cout << strtol("100", &stop, 4) << endl; //16
cout << strtol("13", &stop, 7) << endl; //10
cout << strtol("129dsf", &stop, 2) << endl; //1
cout << stop << endl; //29dsf
cout << strtol("129dsf", &stop, 8) << endl; //10
cout << stop << endl; //9dsf
cout << strtol("129dsf", &stop, 10) << endl; //129
cout << stop << endl; //dsf
cout << strtol("129dsf", &stop, 16) << endl; //4765
cout << stop << endl; //sf
cout << strtol("129dsf", &stop, 30) << endl; //26175555
cout << stop << endl; //
return 0;
}
参考:
http://www.cnblogs.com/lzjsky/archive/2011/01/05/1926369.html
补充:字符串与整数之间的相互转换。
- char* -> int/long long/float:
- sscanf(),同上所述
- atoi()/atoll()/atof()
#include<bits/stdc++.h>
using namespace std;
int main()
{
printf("%d\n", atoi("12450")); //12450
printf("%lld\n", atoll("1000000000000000000")); //1000000000000000000
printf("%.7f\n", atof("-3.14159265358979")); //-3.1415926
return 0;
}
string -> int:
- 使用c_str()函数后同上。
int/long long/float -> char*
- sprintf()
- itoa(),仅支持windows平台,详情可自行百度
int -> string
- stringstream
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 30;
stringstream ss;
ss << aa;
string s1 = ss.str();
cout << s1 << endl; // 30
string s2;
ss >> s2;
cout << s2 << endl; // 30
return 0;
}