[英]Convert decimal to hexadecimal value
我編寫了C代碼,以從十進制轉換為十六進制,但是我編譯了一個C代碼,它僅顯示10到15的十六進制值,表示A到F。請參見下面的代碼。
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;
}
在C語言中將十進制轉換為十六進制
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
long int n,n1,rem;
char *ans="\0",*ch;
clrscr();
printf("\nEnter Your Decimal No :: ");
scanf("%ld",&n);
n1=n;
while(n>0)
{
rem=n%16;
ch=NULL;
sprintf(ch,"%s",((rem==10)?"A":(rem==11)?"B":(rem==12)?"C":(rem==13)?"D":(rem==14)?"E":(rem==15)?"F":"Z"));
if(strcmp(ch,"Z")==0)
sprintf(ch,"%ld",rem);
strcat(ans,ch);
n=n/16;
}
printf("\nYour Decimal No is :: %ld",n1);
printf("\nConvert into Hexadecimal No is :: %s",strrev(ans));
printf("\n\n\n\tThank You");
getch();
}
請格式化代碼。
也許這是一個功課,但如果你只是對轉換感興趣,那就這樣做吧。
int decNum;
scanf("%d",&decNum);
print("%x\n",decNum);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.