gpt4 book ai didi

将十进制数转换为十六进制数

转载 作者:太空狗 更新时间:2023-10-29 15:11:17 25 4
gpt4 key购买 nike

为什么我们使用 + 55 将十进制数转换为十六进制数。在这段代码中,我们使用 +48 将整数转换为字符。当温度 < 10 时。但是当 temp > =10 我们使用 +55 。 +55 是什么意思?

#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];

printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);

quotient = decimalNumber;

while(quotient!=0){
temp = quotient % 16;

//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;

hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}

printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);

return 0;
}

最佳答案

在 ASCII 环境中,55 等于 'A' - 10。这意味着加 55 与减去 10 并加 'A' 相同。

在 ASCII 中,'A''Z' 的值是相邻且连续的,因此这会将 10 映射到 'A'、11 到 'B' 等等。

关于将十进制数转换为十六进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16928517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com