如何执行以下十六进制转换:
- 将值
10作为十六进制转换为A - 将值转换
11为B - 将值转换
12为C - 将值转换
13为D - 将值转换
14为E - 将值转换
15为F
十六进制数 11 16相当于十进制数17 10 。您不能将其“转换”为 b 16,因为它是十进制的 11 10 ,这是不一样的。
目前还不清楚您要完成什么。
如果你这样做:
const int seventeen = 0x11; /* Hexadecimal integer literal. */
或这个:
const int seventeen = 17; /* Decimal integer literal, with the same value. */
那么这与执行以下操作不同:
const int eleven = 0xb;
或这个:
const int eleven = 11;
在分配给变量的内存中设置的值是不同的:
if(seventeen != eleven)
printf("they're not the same!\n");
所以上面会打印“他们不一样!”。
如果要将十进制数打印为十六进制,请使用"%x"格式代码:
int value = 11;
printf("Value is %d decimal, %x hexadecimal\n", value, value);
如果你想从0x11to 0xb,那么唯一的方法就是减去6:
int value = 0x11;
printf("Before: Value = %d, or 0x%02x\n", value, value);
value -= 6;
printf("After : Value = %d, or 0x%02x\n", value, value);
以上将打印
之前:值 = 17,或 0x11 之后:值 = 11,或 0x0b
您不需要任何转换来显示 的十六进制表示int:
int value = 11;
printf("0x%X", value);
输出0xB。就像:
int value = 0xB;
printf("%d", value);
输出11自 11 10 = B 16 ~> 0xB
您还写道,您有十六进制值11,并且想要将其转换为B(实际上是将十进制值 11 转换为十六进制值 0xB)。但如果这11已经是十六进制值 ( 0x11),即 11 16 ~> 它的十进制值将是 1 x 16 1 + 1 x 16 0 = 16 + 1 = 17。
char to_hex(char i)
{
if(i >= 16 || i < 0) return 0;
else if(i >= 10) return i - 10 + 'A';
else return i + '0';
}