进制转换
指定格式输出
- printf
- printf("%05o\n",32); // 八进制格式, 保留 5 位, 高位补零
- printf("%03d\n",32); // 十进制格式, 保留 3 位, 高位补零
- printf("%05x\n",32); // 十六进制格式, 保留 5 位, 高位补零
- cout
- cout <<dec << 32 << endl; // 十进制, dec 可以省略
- cout << hex << 32 << endl; // 十六进制
- cout << oct << 32 << endl; // 八进制
- cout << bitset<8> (32) <<endl; // 二进制, 保留八位, 需要头文件 < bitset>, 详细在下面 bitset 中
手写进制转换
手写各种花式转换不再赘述, 这里主要提一下二进制, 八进制, 十六进制相互转换的技巧, 8 = 2 ^ 3, 16 = 2 ^ 4
二进制转八进制
所以二进制转换成八进制的时候, 只需要将二进制的表示从右往左开始, 每三位二进制数为 1 组 , 分到最后如果不足 3 位, 那么剩下多少位就是多少位, 再用每组的二进制的每一位数从右往左依次乘以 20,21,2^2 然后相加, 得出一组的结果, 最后将所有组的结果相连 (不是相加)
二进制转换为十六进制
二进制转换为十六进制就是将二进制每四位二进制为一组, 其他与八进制转换为二进制一样.
八进制转换为二进制
只需要将八进制的每一个数用三位二进制表示, 然后相连既可以.
十六进制转换为二进制
只要需要将十六进制的每一个数用四位二进制表示, 然后相连即可.
八进制和十六进制相互转换
以二进制作为中转
可以用于进制转换的字符串流和函数
sprintf
可以将一个 10 进制数转换为指定格式的 n 进制字符串
优点: 可以转换各种进制的数.
缺点: 需要先分配足够的 char 数组.
函数原型
int sprintf( char *buffer, const char *format, [ argument] ... );
原型说明:
buffer:char 型指针, 指向将要写入的字符串的缓冲区.
format: 格式化字符串.(格式控制)
[argument]...: 可选参数, 可以是任何类型的数据.
返回值: 字符串长度 (strlen)
如:
- #include <cstdio>
- #include <iostream>
- #include <string.h>
- using namespace std;
- int main() {
- int aa = 30;
- char c[8];
- int length = sprintf(c, "%05X", aa);
- cout <<c << endl; // 0001E
- return 0;
- }
还有网上 copy 的一份应用:
- #include<cstdio>
- int main() {
- char s[100]={0};
- sprintf(s, "%d", 123); // 十进制输出产生 "123"
- sprintf(s, "%4d%4d", 123, 4567); // 指定宽度不足的左边补空格, 产生:"1234567"
- sprintf(s, "%8o", 123); // 八进制输出, 宽度占 8 个位置
- sprintf(s, "%8x", 4567); // 小写 16 进制, 宽度占 8 个位置, 右对齐
- sprintf(s, "%10.3f", 3.1415626); // 产生:"3.142"
- int i = 100;
- sprintf(s, "%.2f", i); // 注意这是不对的
- sprintf(s, "%.2f", (double)i); // 要按照这种方式才行
- return 0;
- }
- stringstream
需要头文件 #include 缺点: 慢
十进制转八, 十六进制
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <sstream>
- using namespace std;
- int main() {
- string s, s2;
- long long x;
- stringstream ss;
- cin>> s;
- ss <<hex << s;
- ss>> x;
- cout <<x << endl;
- return 0;
- }
八, 十六进制转十进制
- #include <iostream>
- #include <string.h>
- #include <sstream>
- using namespace std;
- int main() {
- string s = "11";
- int a;
- stringstream ss;
- ss <<hex << s; // 以 16 进制读入流中
- ss>> a; //10 进制 int 型输出
- cout <<a << endl;
- return 0;
- }
- //
十六, 八进制相互转换
如果数字不大的话可以以十进制作为中转, 超 long long 的见手打十六, 八, 二进制的相互转换
- strtol
- string to long
函数原型
long int strtol(const char *nptr, char **endptr, int base)
原型说明:
base 是要转化的数的进制
非法字符会赋值给 endptr
nptr 是要转化的字符
如:
- #include <cstdio>
- #include <iostream>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- int main() {
- string s = "10549stend#12";
- char *stop;
- int ans = strtol(s.c_str(), &stop, 8);
- cout <<oct << ans << endl;
- cout << ans << endl;
- cout << stop << endl;
- return 0;
- }
- Tips:
[1] 如果 base 为 0, 且字符串不是以 0x(或者 0X) 开头, 则按十进制进行转化.
[2] 如果 base 为 0 或者 16, 并且字符串以 0x(或者 0X) 开头, 那么, x(或者 X) 被忽略, 字符串按 16 进制转化.
[3] 如果 base 不等于 0 和 16, 并且字符串以 0x(或者 0X) 开头, 那么 x 被视为非法字符.
[4] 对于 nptr 指向的字符串, 其开头和结尾处的空格被忽视, 字符串中间的空格被视为非法字符.
itoa
可以将一个 10 进制数转换为任意的 2-36 进制字符串 (即可以完成进制转换和 int 转 string 两个功能)
重要的注意要先说: itoa 并不是一个标准的 C/C++ 函数, 它是 Windows 特有的
函数原型
char *itoa( int value, char *string,int radix);
原型说明:
value: 欲转换的数据.
string: 目标字符串的地址.
radix: 转换后的进制数
返回指向 string 这个字符串的指针
如:
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <string.h>
- using namespace std;
- int main() {
- int num = 10;
- string s;
- char str[110];
- _itoa(num, str, 2); //c++ 中一般用_itoa, 用 itoa 也行,
- s = str;
- cout <<s << endl;
- return 0;
- }
- bitset
需包含头文件 #include
构造函数
如 (转自 https://www.cnblogs.com/magisk/p/8809922.html)
- bitset<4> bitset1; // 无参构造, 长度为4, 默认每一位为0
- bitset<8> bitset2(12); // 长度为8, 二进制保存, 前面用0补充
- string s = "100101"; // 只能是 01 串
- bitset<10> bitset3(s); // 长度为 10, 前面用0补充
- char s2[] = "10101";
- bitset<13> bitset4(s2); // 长度为 13, 前面用0补充
- bitset<2> bitset5(12); //12 的二进制为 1100(长度为4), 但 bitset1 的 size=2, 只取后面部分, 即 00
- cout <<bitset1 << endl; //0000
- cout << bitset2 << endl; //00001100
- cout << bitset3 << endl; //0000100101
- cout << bitset4 << endl; //0000000010101
- cout << bitset5 << endl; //00
支持下标访问, 最低为下标为 0
常用函数
.count() 用来求有几个 1
.size() 求大小 (位数)
.test(i) 判断下标为 i 的的元素是 0 还是 1,1 返回 true, 0 返回 false
.any() 判断是否含 1
.none() 判断是否不含 1
.all() 判断是否都是 1
如 (转自 https://www.cnblogs.com/magisk/p/8809922.html):
- bitset<8> foo ("10011011");
- cout <<foo.count() << endl; //5 (count 函数用来求 bitset 中 1 的位数, foo 中共有5个1
- cout << foo.size() << endl; //8 (size 函数用来求 bitset 的大小, 一共有8位
- cout << foo.test(0) << endl; //true (test 函数用来查下标处的元素是0还是1, 并返回 false 或 true, 此处 foo[0] 为1, 返回 true
- cout << foo.test(2) << endl; //false (同理, foo[2] 为0, 返回 false
- cout << foo.any() << endl; //true (any 函数检查 bitset 中是否有1
- cout << foo.none() << endl; //false (none 函数检查 bitset 中是否没有1
- cout << foo.all() << endl; //false (all 函数检查 bitset 中是全部为1
int string 相互转换
int 转 string
itoa/stringstream/sprintf 见上
string 转 int
strtol/stringstream 见上
sscanf
详见百科:
大小写转换
char 转换 toupper, tolower
transform
原型
- template < class InputIterator, class OutputIterator,
- class UnaryOperator>
- OutputIterator transform ( InputIterator first1, InputIterator last1,
- OutputIterator result, UnaryOperator op );
- template <class InputIterator1, class InputIterator2,
- class OutputIterator, class BinaryOperator>
- OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
- InputIterator2 first2, OutputIterator result,
- BinaryOperator binary_op );
如 (转自 https://www.cnblogs.com/balingybj/p/4678850.html)
- #include <string>
- #include <algorithm>
- using namespace std;
- int main()
- {
- string strA = "[email protected]";
- string strB = "[email protected]";
- printf("Before transform:\n");
- printf("strA:%s \n", strA.c_str());
- printf("strB:%s \n\n", strB.c_str());
- transform(strA.begin(), strA.end(), strA.begin(), ::toupper);
- transform(strB.begin(), strB.end(), strB.begin(), ::toupper);
- printf("After transform to toupper:\n");
- printf("strA:%s \n", strA.c_str());
- printf("strB:%s \n\n", strB.c_str());
- transform(strA.begin(), strA.end(), strA.begin(), ::tolower);
- transform(strB.begin(), strB.end(), strB.begin(), ::tolower);
- printf("After transform to lower:\n");
- printf("strA:%s \n", strA.c_str());
- printf("strB:%s \n\n", strB.c_str());
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3676785.html