C语言 将十进制数转换为十六进制数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16928517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a decimal to a hexadecimal number
提问by MD MAHBUB ALAM
Why we use + 55 for converting decimal to hex num . in this code we use +48 to convert integer to character . when temp < 10 . But when temp > =10 we use +55 . what does it mean by +55 ?
为什么我们使用 + 55 将十进制转换为十六进制 num 。在这段代码中,我们使用 +48 将 integer 转换为 character 。当温度 < 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;
}
回答by caf
In an ASCII environment, 55 is equal to 'A' - 10. This means that adding 55 is the same as subtracting 10 and adding 'A'.
在 ASCII 环境中,55 等于'A' - 10. 这意味着加 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'through的值'Z'是相邻且连续的,因此这将映射 10 到'A'、11 到'B'等等。
回答by John
For values of templess 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 letteris 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'
回答by Kninnug
Because of the ASCIIencoding 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)小于十时,十六进制中的数字也在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 + 55is 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 到 65 的数字也是70 并因此给出一个在 'A' 到 'F' 范围内的字符。
It is more common to use a string char[] digits = "0123456789ABCDEF";and use the remainder as an index in this string. The method in your question (probably) works as well though.
更常见的是使用一个字符串char[] digits = "0123456789ABCDEF";并将余数用作该字符串中的索引。不过,您问题中的方法(可能)也有效。
回答by programmingsimplysolved.com
Hexadecimal number is a number represented using 16 symbols which are 0 -9 numbers and A – F alphabets. Procedure to write a c program to convert decimal number to hexadecimal is: Divide the decimal number with 16 at each step and take remainders
十六进制数是使用 0 -9 数字和 A - F 字母的 16 个符号表示的数字。编写ac程序将十进制数转换为十六进制数的程序是:每一步将十进制数除以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 . Now combine all remainders serially from down to up i.e A This is hexadecimal number of 10 (decimal )
这里使用 0 – 9 个数字来表示余数,然后我们使用字母 A、B、C、D、E、F 来表示 10 到 15 个数字。现在将所有余数从下到上依次组合,即 A 这是 10 的十六进制数(十进制)
eg To convert decimal number 10 to hexadecimal
例如,将十进制数 10 转换为十六进制
16 | 10 | 0 – A
16 | 10 | 0 – 一个
Now combine all remainders serially from down to up i.e A . This is hexadecimal number of 10 (decimal )
现在从下到上依次组合所有余数,即 A 。这是 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/
contributed by: www.programmingsimplysolved.com
贡献者:www.programmingsimlysolved.com

