Decimal to hexadecimal (floating point to hexadecimal) C language code + 2 methods

Introduction to Hexadecimal

        Hexadecimal (abbreviated as hex or subscript 16), in mathematics, is a base system in which every 16 is entered. Usually 0-9, af (or AF) represent 16 base numbers, that is, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F represent 0 -15. For example: hexadecimal number 1F, F is the unit digit means 15, 1 is the tens digit means 1 16, then it is converted to decimal number is 31, 16+15=31. Hexadecimal number is commonly used in computers The base number system, such as the color code #FFFFFF, Unicode code U+200D, etc., can be expressed in hexadecimal numbers.

Decimal to hexadecimal conversion principle

        Decimal decimal to hexadecimal decimal, the integer part method is divided by sixteen and the remainder is reversed, the decimal part method is multiplied by sixteen and rounded, and the integer part of the result is obtained after multiplying by the corresponding base number each time. It should be noted that not all decimal decimals can be completely converted into hexadecimal decimals, and an approximation is required in this case.

Take 1234.567 as an example:

Expansion: Convert decimal decimals to other decimals

         The method of converting decimal decimals to other bases is the same as converting to hexadecimal. The method of converting decimal decimals to R decimals is to divide the integer part by taking the remainder and then reverse the order. The method of the decimal part is to multiply by R and round up. Multiply by the corresponding base and take the integer part of the result. It should be noted that not all decimal decimals can be completely converted into R decimals, and an approximate value needs to be taken at this time.

C language code example

method one:

#include <stdio.h>
#include <string.h>

void main()
{
	char integer[32]="0";
    char decimal[32]="0";
    char HEX[] = "0123456789ABCDEF";
    char hex[64];
    char hex_temp;
    int i,j,k = 0;
    double num, decimal_temp ,integer_temp = 0;
   	printf("输入一个小数:");
   	scanf("%lf", &num);
    integer_temp = num;

    while (integer_temp)
    {
		integer[i++] = HEX[(int)integer_temp % 16];   //对十进制数求余并最终与HEX[]数组中的字符匹配
		integer_temp = (int)integer_temp / 16;
    }

    integer[i]='.';

    for(j = i - 1; j-k > 0; j--)//调换整数的16进制顺序
    {
		hex_temp = integer[j];
		integer[j] = integer[k++];
		integer[i-j-1] = hex_temp;
    }

    decimal_temp = num-(int)num;

    for(i = 0; i < 6;i++)//保留6位有效数字
    {
		decimal_temp = decimal_temp * 16;
        decimal[i] =  HEX[(int)decimal_temp];
		decimal_temp = decimal_temp -(int)decimal_temp;
		if(decimal_temp == 0) 
		{
		   decimal[i+1]='\0';
			break;
		}
    }
	strcpy(hex,integer);
	strcat(hex,decimal);
	puts(hex);
}

Verification: Online base conversion tool

Method Two:

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
   char out[20]="0";
   char out1[20]="0";
   double temp;
   double data;
   int integer=0,i=0;
   double decimal=0;
   printf("输入一个小数:");
   scanf("%lf",&data);
   integer = (int)data;
   decimal = data - integer;

   for(i = 0; i < 10;i++)
   {
	decimal = decimal*16;
        sprintf(out1+i,"%X",(int)decimal);
	temp = decimal -(int)decimal;
	decimal=temp;
	if(temp == 0) 
        {
	   out1[i+1]='\0';
           break;
        }
   }
   sprintf(out,"%X.",integer);
   strcat(out,out1);
   printf("out=%s\n",out);

return 0;
}

Verification: Online base conversion tool
       

Floating point stored in IEEE 754 standard format to HEX:

Point number (decimal) to hexadecimal C language code example + principle explanation (mandatory type conversion + union conversion)_xiaohai@Linux的博客-CSDN博客

Guess you like

Origin blog.csdn.net/qq_34885669/article/details/122423960