61 lines
1.2 KiB
C++
61 lines
1.2 KiB
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
using namespace std;
|
|||
|
|
const int N = 110;
|
|||
|
|
int a[N], al;
|
|||
|
|
string s[N];
|
|||
|
|
int sl;
|
|||
|
|
|
|||
|
|
// 2进制转16进制 0011 1100
|
|||
|
|
char get(string x) { // x 必须是4位一组的01数字 --> E B A
|
|||
|
|
int sum = 0;
|
|||
|
|
int base = 1;
|
|||
|
|
for (int i = x.size() - 1; i >= 0; i--) {
|
|||
|
|
sum += (x[i] - '0') * base;
|
|||
|
|
base *= 2;
|
|||
|
|
}
|
|||
|
|
if (sum >= 10)
|
|||
|
|
return (char)(sum - 10 + 'A');
|
|||
|
|
return (char)(sum + '0');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// x=0000 0111
|
|||
|
|
// 0E
|
|||
|
|
string getHex(string x) { // 0E FE AC
|
|||
|
|
string a = x.substr(0, 4); // 前4个
|
|||
|
|
string b = x.substr(4, 4); // 后4个
|
|||
|
|
string c;
|
|||
|
|
c.push_back(get(a));
|
|||
|
|
c.push_back(get(b));
|
|||
|
|
return c;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int main() {
|
|||
|
|
long long n = 987654321012345678;
|
|||
|
|
while (n) {
|
|||
|
|
int x = n % 2;
|
|||
|
|
n /= 2;
|
|||
|
|
a[++al] = x;
|
|||
|
|
}
|
|||
|
|
// 需要添加若干0来完成补位,需要添加多少个?
|
|||
|
|
int x = 7 - al % 7;
|
|||
|
|
if (al % 7 == 0)
|
|||
|
|
x = 0;
|
|||
|
|
al += x;
|
|||
|
|
|
|||
|
|
for (int i = al; i >= 1; i -= 7) { // 每一组
|
|||
|
|
string q = "";
|
|||
|
|
if (i == 7)
|
|||
|
|
q = q + "1";
|
|||
|
|
else
|
|||
|
|
q = q + "0";
|
|||
|
|
for (int j = 0; j < 7; j++) // 每一个
|
|||
|
|
q = q + to_string(a[i - j]);
|
|||
|
|
|
|||
|
|
s[++sl] = q;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (int i = sl; i; i--)
|
|||
|
|
cout << getHex(s[i]) << " ";
|
|||
|
|
|
|||
|
|
return 0;
|
|||
|
|
}
|