小编MD *_*LAM的帖子

将小数转换为十六进制数

为什么我们使用+ 55将十进制转换为十六进制数.在这段代码中,我们使用+48将整数转换为字符.当temp <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;
}
Run Code Online (Sandbox Code Playgroud)

c hex decimal number-formatting

4
推荐指数
3
解决办法
7万
查看次数

十进制到二进制转换输出

我的代码有什么问题,任何人帮助我plz.这是一个十进制到二进制的转换.根据我的代码,输出将是2为10,3为11但它输出总是添加最后一个值,如3显示1110,添加以前的输出.我现在应该怎么做 ?帮我PLZ?

#include<iostream>
#include<stdio.h>
using namespace std;

int main(){

    long int decimalNumber,quotient;

    int binaryNumber[100],i=0,j;

    printf("Enter any decimal number: ");

    //scanf_s("%ld",&decimalNumber);
    while(scanf_s("%ld",&decimalNumber)==1)
    {

            quotient = decimalNumber;

            while(quotient!=0){
             binaryNumber[i++]= quotient % 2;
             quotient = quotient / 2;
           }

           printf("Equivalent binary value of decimal number %d: ",decimalNumber);

           for(j = i -1 ;j>= 0;j--)
               printf("%d",binaryNumber[j]);
           printf("\n");
           printf("Enter any decimal number: ");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ binary decimal

0
推荐指数
1
解决办法
463
查看次数

标签 统计

c ×2

decimal ×2

binary ×1

c++ ×1

hex ×1

number-formatting ×1