您应该将每个单独的 4 位组转换为一个字符,例如。有一张桌子:
char Hex[16]={ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
一种通用的计算方法(在任何 2 个 BASES 之间没有库)是:
步骤 1)
my_number = 0; // start to read digits.
while((a = read_digit(BASE))>=0) {
// read_digit should return -1 for non digit
my_number=my_number * BASE + a;
// optionally: if (count++ == 4) break; /* read only 4 bits or digits */
}
第二步)
// convert my_number to another base (eg. hex or decimal or octal or Base64)
// by repeated division by BASE and storing the modulus
while (my_number) {
int modulus = my_number % BASE;
my_number/=BASE;
PUSH(modulus);
}
步骤 2.5)
// Pop the numbers in range (0..BASE-1) from stack
// to output them from left to right
while (POP(my_number) >=0) { // assumes that pop returns <0 when empty
cout << myTable[my_number]; // this can be selected for any base
}