简体   繁体   English

将小数转换为十六进制数

[英]Converting a decimal to a hexadecimal number

Why we use + 55 for converting decimal to hex num . 为什么我们使用+ 55将十进制转换为十六进制数。 in this code we use +48 to convert integer to character . 在这段代码中,我们使用+48将整数转换为字符。 when temp < 10 . 当temp <10。 But when temp > =10 we use +55 . 但是当temp> = 10时,我们使用+55。 what does it mean by +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;
}

In an ASCII environment, 55 is equal to 'A' - 10 . 在ASCII环境中,55等于'A' - 10 This means that adding 55 is the same as subtracting 10 and adding 'A' . 这意味着添加55与减去10并添加'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. 在ASCII中, 'A''Z'是相邻的和顺序的,因此这将把10映射到'A' ,11映射到'B' ,依此类推。

For values of temp less than 10, the appropriate ASCII code is 48 + temp : 对于temp小于10的值,相应的ASCII代码为48 + temp

0 => 48 + 0 => '0'
1 => 48 + 1 => '1'
2 => 48 + 2 => '2'
3 => 48 + 3 => '3'
4 => 48 + 4 => '4'
5 => 48 + 5 => '5'
6 => 48 + 6 => '6'
7 => 48 + 7 => '7'
8 => 48 + 8 => '8'
9 => 48 + 9 => '9'

For values 10 or greater, the appropriate letter is 55 + temp : 对于值10或更大,相应的字母55 + temp

10 => 55 + 10 => 'A'
11 => 55 + 11 => 'B'
12 => 55 + 12 => 'C'
13 => 55 + 13 => 'D'
14 => 55 + 14 => 'E'
15 => 55 + 15 => 'F'

Because of the ASCII encoding of characters in C. When the remainder ( temp ) is less than ten, the digit in the hexadecimal is also in the range of 0 to 9. The characters '0' to '9' are on the ASCII range of 48 to 57. 由于C中字符的ASCII编码。当余数( temp )小于10时,十六进制中的数字也在0到9的范围内。字符'0'到'9'在ASCII范围内48至57。

When the remainder is more than 10 (and always less than 15, due to the remainder operation % ) the hexadecimal digit is in the range A to F, which in ASCII is in the range of 65 to 70. So temp + 55 is a number from 65 to 70 and thus gives a character in the range of 'A' to 'F'. 当余数大于10(并且总是小于15,由于余数运算% ),十六进制数字在A到F的范围内,在ASCII中的范围是65到70.所以temp + 55是数字从65到70,因此给出“A”到“F”范围内的字符。

It is more common to use a string char[] digits = "0123456789ABCDEF"; 更常见的是使用字符串char[] digits = "0123456789ABCDEF"; and use the remainder as an index in this string. 并使用余数作为此字符串中的索引。 The method in your question (probably) works as well though. 你问题中的方法(可能)也可以。

Hexadecimal number is a number represented using 16 symbols which are 0 -9 numbers and A – F alphabets. 十六进制数是使用16个符号表示的数字,它们是0-9个数字和A-F字母表。 Procedure to write ac program to convert decimal number to hexadecimal is: Divide the decimal number with 16 at each step and take remainders 编写交流程序以将十进制数转换为十六进制的过程是:在每一步中将十进制数除以16并取剩余数

Here for remainders 0 – 9 numbers are used and then to represent 10 to 15 numbers we use alphabets A, B, C, D, E, F . 这里使用剩余的0到9个数字然后代表10到15个数字我们使用字母A,B,C,D,E,F。 Now combine all remainders serially from down to up ie A This is hexadecimal number of 10 (decimal ) 现在从下到上依次组合所有余数,即A这是十六进制数10(十进制)

eg To convert decimal number 10 to hexadecimal 例如,要将十进制数10转换为十六进制

16 | 16 | 10 | 10 | 0 – A 0 - A.

Now combine all remainders serially from down to up ie A . 现在将所有剩余部分从下到上连续组合,即A. This is hexadecimal number of 10 (decimal ) 这是十六进制数10(十进制)

Program Logic: Enter decimal number n, divide n by 16 ( since hexadecimal ) and save remainder in array and quotient in n repeat until n is greater than zero 程序逻辑:输入十进制数n,将n除以16(因为十六进制)并将余数保存在数组中,将商保存在n重复中,直到n大于零

view program at: http://www.programmingsimplysolved.com/c-programs/c-program-to-convert-decimal-number-to-hexadecimal-using-functions/ 查看计划: http//www.programmingsimplysolved.com/c-programs/c-program-to-convert-decimal-number-to-hexadecimal-using-functions/

contributed by: www.programmingsimplysolved.com 贡献者:www.programmingsimplysolved.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM