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

问题描述

我写C code到十进制到十六进制数转换,但我编译之一,它展示的10到15意味着A到F请参见下面我code只有十六进制值。

 的main()
{
    INT N,R [10],I,D = 0,E = 1;
    的printf(请输入十进制数\\ n);
    scanf函数(%d个,&安培; N);
    对于(i = 0; I< N;我++)
    {
         - [R [i] = N%16;
        N = N / 16;
        D = D +(R [I] * E);
        E = E * 10;
    }
    一世 - ;
    对于(I = N; I> = 0;我 - )
    {
        如果(R [I] == 10)
            的printf(A);
        否则如果(R [I] == 11)
            的printf(B);
        否则如果(R [I] == 12)
            的printf(C);
        否则如果(R [I] == 13)
            的printf(D);
        否则如果(R [I] == 14)
            的printf(E);
        否则如果(R [I] == 15)
            的printf(F);
        其他
            的printf(六十进制值%d个\\ N,D);
    }
}


解决方案

 的#include<&stdio.h中GT;诠释主要(无效){
  INT N;  如果(scanf函数(%d个,&安培; N)== 1){
    的printf(十六进制:%X \\ N,N);
  }
  返回0;
}

I wrote C code to convert from decimal to hexadecimal, but i compile that one it show only the hexadecimal value of 10 to 15 that mean A to F. Please see my code below.

main()
{
    int n,r[10],i,d=0,e=1;
    printf("Enter the decimal number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        r[i]=n%16;
        n=n/16;
        d=d+(r[i]*e);
        e=e*10;
    }
    i--;
    for(i=n;i>=0;i--)
    {
        if(r[i]==10)
            printf("A");
        else if(r[i]==11)
            printf("B");
        else if(r[i]==12)
            printf("C");
        else if(r[i]==13)
            printf("D");
        else if(r[i]==14)
            printf("E");
        else if(r[i]==15)
            printf("F");
        else
            printf("hexa decimal value %d\n",d);
    }
}
解决方案
#include <stdio.h>

int main(void) {
  int n;

  if (scanf("%d", &n) == 1) {
    printf("hexadecimal: %x\n", n);
  }
  return 0;
}

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

08-15 00:35