我正在爲一所學校的項目工作,所以即時通訊不尋找某人爲我做我的工作,但我無法弄清楚這一點。我需要編寫一個程序,將基數爲10的數字轉換爲二進制和十六進制。我無法弄清楚爲什麼我的代碼不打印出二進制數字。結果總是打印0001並且不採取整個輸出。我不好解釋這是誠實的,但繼承人的代碼。不正確打印二進制
#include<stdio.h>
#define MAX 1000
int main()
{
long int baseTenNum,remainder,hexQuotient;
int i=1,j,temp;
char hexNum[MAX], binaryNum[MAX];
printf("Enter any Base10 number: ");
scanf("%ld",&baseTenNum);
// quotient variable to convert to hexidecimal value
hexQuotient = baseTenNum;
// while loop to get hexidecimal value
while(hexQuotient!=0)
{
temp = hexQuotient % 16;
// Converts integer to character
if(temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexNum[i++]= temp;
hexQuotient = hexQuotient/16;
}
printf("\nhexadecimal value of base 10 number %d: ",baseTenNum);
for(j = i -1 ;j> 0;j--)
printf("%c",hexNum[j]);
printf("\n\n");
if (hexNum[j] = 0){
printf("0000");
}
else if(hexNum[j] = 1){
printf("0001");
}
else if(hexNum[j] = 2){
printf("0010");
}
else if(hexNum[j] = 3)
{
printf("0011");
}
else if(hexNum[j] = 4){
printf("0100");
}
else if(hexNum[j] = 5){
printf("0101");
}
else if(hexNum[j] = 6){
printf("0110");
}
else if(hexNum[j] = 7){
printf("0111");
}
else if(hexNum[j] = 8){
printf("1000");
}
else if(hexNum[j] = 9){
printf("1001");
}
else if(hexNum[j] = "A"){
printf("1010");
}
else if(hexNum[j] = "B"){
printf("1011");
}
else if(hexNum[j] = "C"){
printf(1100);
}
else if(hexNum[j] = "D"){
printf("1101");
}
else if(hexNum[j] = "E"){
printf("1110");
}
else if(hexNum[j] = "F"){
printf("1111");
}
printf("\n%c",hexNum[2]);
return 0;
}
當心:1和 '1' 和 「1」 都非常* *不同 –
1是整數1時, '1' 是A *的char * ASCII值不是數字1,和「1」是一個*指針*到一個常量字符數組,這將不能很好地與比較 –