Files
python/GESP/资料/灰阶图像/十六进制转换测试.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

49 lines
1009 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

include <bits/stdc++.h>
using namespace std;
//将一个16进制的灰阶转为一个整数,方便用来当数组的下标索引值
int toDec(char a, char b) {
int res = 0;
if (a >= '0' && a <= '9')
res += (a - '0') * 16;
else if (a >= 'A' && a <= 'F')
res += (a - 'A' + 10) * 16;
if (b >= '0' && b <= '9')
res += b - '0';
else if (b >= 'A' && b <= 'F')
res += b - 'A' + 10;
return res;
}
//将一个十进制的灰阶值[0,255]转化成十六进制[0,FF]
string toHex(int x) {
char c1, c2;
int a = x % 16; // 0 ~ 15
int b = x / 16; //
if (a >= 10)
c1 = 'A' + a - 10;
else
c1 = '0' + a;
if (b >= 10)
c2 = 'A' + b - 10;
else
c2 = '0' + b;
string res;
res.push_back(c2);
res.push_back(c1);
return res;
}
int main() {
cout << toDec('A', 'B') << endl;// AB-->? 10*16+ 11 =171
cout << toDec('1', '3') << endl;// 19
cout << toDec('1', 'A') << endl;// 26
cout << toHex(171) << endl; //AB
cout << toHex(19) << endl; //13
cout << toHex(26) << endl; //1A
return 0;
}