1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#include <iostream>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <assert.h>
using namespace std;
string int2nradix(long long x, long long n){
int digit;
char ch;
string res;
do{
digit = x%n;
ch = digit>=10?('a'+(digit-10)):('0'+digit);
res = ch + res;
x /= n;
}while(x);
return res;
}
long long nradix2int(string &seq, long long n){
long long res = 0;
for(int i=0; i<seq.size(); ++i){
if(isalpha(seq[i])){
res = res*n + (tolower(seq[i]) - 'a') + 10;
}
else{
res = res*n + (seq[i] - '0');
}
}
return res;
}
void test1(){
bool flag = false;
srand((int)time(NULL));
for(int i=0; i<100000; ++i){
int n = abs(rand())%36;
n = (n==0||n==1)?2:n;
string seq = int2nradix(i, n);
if(i != nradix2int(seq, n)){
cout<<"error: "<<i<<" "<<seq<<" "<<nradix2int(seq, n)<<", 进制为: "<<n<<endl;
flag = true;
}
else{
// cout<<i<<"的"<<n<<"进制为"<<seq<<endl;
}
}
if(!flag){
cout<<"全部正确"<<endl;
}
}
void test2(){
srand((int)time(NULL));
char buf[1024];
for(int i=0; i<100000; ++i){
int n = rand() % 36;
n = (n==0||n==1)?2:n;
memset(buf, 0, sizeof(buf));
string s1 = int2nradix(i, n);
itoa(i, buf, n);
string s2(buf);
assert(s1==s2);
long long x = nradix2int(s2, n);
assert(x==i);
}
}
int main(){
// c语言中输出
printf("%o\n", 16); // 8进制, 输出20
printf("%d\n", 16); // 10进制, 输出16
printf("%x\n", 16); // 16进制, 输出10
// atoi() c语言函数, 将10进制数字符串转换为10进制数
// int atoi(const char *str)
char s1[] = "12344321";
cout<<atoi(s1)<<endl;
// strtol() c函数 将一个任意进制的字符串转换为10进制数(int型)
// endptr指向非法字符所在的位置
// long int strtol(const char *nptr, char **endptr, int base)
char *ptr;
cout<<strtol(s1, &ptr, 5)<<endl;
cout<<ptr<<endl; // 无内容
cout<<strtol(s1, &ptr, 4)<<endl;
cout<<ptr<<endl; // 因为进制为4, 所以原字符串中4为非法, 此处输出44321
// itoa() c函数, 不是标准库函数. 受字母表限制, 只能转换2~36进制(10个阿拉伯字母+26个英文字母)
// char*itoa(int value,char*string,int radix);
char s2[10];
itoa(16, s2, 16); // 将16转换为16进制
cout<<s2<<endl; // 输出10
itoa(71, s2, 36); // 将71转换成36进制
cout<<s2<<endl; //输出1z
// sprintf() c函数, 字符串格式化函数, 可以将10进制转换为8进制, 16进制
// int sprintf(char *string, char *format [,argument,...]);
// %[flags][width][.precision][length]specifier
sprintf(s2, "%d", 12344321); // 将12344321写入s2中
cout<<s2<<endl;
sprintf(s2, "I eat %d apples.", 1234);
cout<<s2<<endl; // 输出I eat 1234 apples.
sprintf(s2, "%.2f", (double)123);
cout<<s2<<endl; // 输出123.00
sprintf(s2, "%x", 15);
cout<<s2<<endl; // 输出f
sprintf(s2, "%X", 15);
cout<<s2<<endl; // 输出F
// c++ 按指定格式输出 (注意这样使用后, 需要清除这些标志位, 如在代码的60行必须重置回来)
cout<<oct<<16<<endl; // 把16按8进制输出, 即20
cout<<dec<<16<<endl; // 把16按10进制输出, 即16
cout<<hex<<16<<endl; // 把16按16进制输出, 即20
cout<<bitset<8>(16)<<endl; // 用长为8bitset将16按2进制输出, 即00010000
// stoi() c++函数, 将所给的base进制的字符串str转换为10进制
// idx指示的是第一个不合法字符所在的位置
// int stoi (const string& str, size_t* idx = 0, int base = 10);
string st = "20 dddd";
size_t sz;
cout<<dec<<stoi(st, &sz, 10)<<endl;
cout<<"不合法字段为: "<<st.substr(sz)<<endl;
st = "20";
cout<<stoi(st, nullptr, 8)<<endl; // 将8进制串20转换为10进制, 即输出16
// 利用stoi()函数可以实现任意进制到十进制的转换
// 利用bitset可以实现10进制到2进制的转换
// 利用itoa()实现从10进制到任意进制的转换
// 需要自己实现从10进制到任意进制的转换
test1(); // 测试自己写的函数
test2();
return 0;
}
|