将十进制转换为十六进制数字 [英] Converting a decimal to a hexadecimal number

查看:127
本文介绍了将十进制转换为十六进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #include< stdio.h> 
int main(){
long int decimalNumber,remainder,quotient;
int i = 1,j,temp;
char十六进制数字[100];

printf(输入任意十进制数:);
scanf(%ld,& decimalNumber);

quotient = decimalNumber; (商数!= 0){
temp = quotient%16;

;

//将整数转换为字符
if(temp <10)
temp = temp + 48;
else
temp = temp + 55;

十六进制数字[i ++] = temp;
quotient = quotient / 16;


printf(十进制数的等价十六进制数%d:,decimalNumber); (j = i-1; j> 0; j-)
printf(%c,十六进制数字[j])的
;

返回0;


解决方案

等于'A' - 10 。这意味着添加55与减去10相同并添加'A'



在ASCII中,值'A''Z'是相邻的和顺序的,所以这将映射10到'A',11至'B'等等。

Why we use + 55 for converting decimal to hex num . in this code we use +48 to convert integer to character . when temp < 10 . But when temp > =10 we use +55 . what does it mean by +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;
}

解决方案

In an ASCII environment, 55 is equal to 'A' - 10. This means that adding 55 is the same as subtracting 10 and adding 'A'.

In ASCII, the values of 'A' through 'Z' are adjacent and sequential, so this will map 10 to 'A', 11 to 'B' and so on.

这篇关于将十进制转换为十六进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆