C language (programming question: convert hexadecimal number to decimal)

C language

topic

Write a function that takes a hexadecimal number as input and outputs the corresponding decimal number.

basic knowledge

1. Hexadecimal definition

-------The hexadecimal system means that every 16 enters 1, and each digit can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F are 16 numbers of different sizes. Hexadecimal conversion is the conversion between hexadecimal and other different bases, such as binary, octal, and decimal.
And the standard representation of hexadecimal is generally 0x followed by numbers

2. Hex conversion :

------- Hexadecimal conversion has hexadecimal each bit can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F16 numbers of different sizes, that is, every 16 enters 1, and the six letters of A, B, C, D, E, F (the letters are not case-sensitive) are used to represent 10, 11, 12, 13, 14, 15.

3. Various comparison tables:
insert image description here

Analysis topic

  • First understand how hexadecimal is converted to decimal by calculation.

--------The weight of the 0th digit of the hexadecimal number is 16 to the 0th power, the weight of the 1st digit is 16 to the 1st power, and the weight of the 2nd digit is 16 to the 2nd power Square... So, on the Nth (N starts from 0) bit, if it is a number X (X is greater than or equal to 0, and X is less than or equal to 15, that is: F), the size expressed is X * 16 to the Nth power.

  • Example: Convert 0x2AF5 to decimal
    5 * 16^0 + F * 16^1 + A * 16^2 + 2 * 16^3 = 10997

programming solution

After analyzing the problem, we know the principles of the two base conversions, and we will also perform the calculation of base conversion. Now we need to convert these mathematical ideas into programming languages.
And there are many small problems in the programming of base conversion, which will be explained in detail.

1. First define the name of the writing function:

int Get_0x(const char* str)//将函数命名为Get_0x 并且形参为不可修改的字符串 

2. The header file used by the base conversion function:

#include <stdio.h>//标准库函数
#include <ctype.h>//特殊字符判断的头文件
#include <string.h>//字符串操作用到的头文件
#include <limits.h>//检测整型数据类型的表达式范围

3. If a space is encountered:

while (isspace(*str))
	{
    
    
		str++;//因为空格在转换过程中对数值没有影响,直接++后移进行后续操作
	}

4. Judgment and solution of positive and negative signs:

int index = 1;
	if (*str == '-' || *str == '+')
	{
    
    
		if (*str == '-')
		{
    
    
			index *= -1;
		}
		else
		{
    
    
			index = 1;
		}
		str++;
	}

5. Determine whether the first two digits are the expression form of 0X hexadecimal:

	if ((*str == '0') && (*(str + 1) == 'x' || *(str + 1) == 'X'))      //判断前两位是否是0X 16进制的表达形式
	{
    
    
		str += 2;     //如果是从第三位开始换算计算
	}
	else
	{
    
    
		return INT_MAX;
	}

6. Determine whether it is a character:

while (isxdigit(*str))        //判断是否是字符
	{
    
    
		if (isdigit(*str))    //如果是字符按位进行操作 这部分就是将数学思想转变为编程语言
		{
    
    
			sum = sum * 16 + (*str - '0');
		}
		else if (islower(*str))
		{
    
    
			sum = sum * 16 + (*str - 'a' + 10);
		}
		else
		{
    
    
			sum = sum * 16 + (*str - 'A' + 10);
		}
		str++;
	}

7. End sign:

if (*str == '\0')
		return sum * index;
	return INT_MAX;

The above is the code of each part of the binary conversion and the functions it realizes.

all codes

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
int Get_0x(const char* str)
{
    
    
	int sum = 0;
	while (isspace(*str))
	{
    
    
		str++;
	}
	//此时 空格处理结束
	int index = 1;
	if (*str == '-' || *str == '+')
	{
    
    
		if (*str == '-')
		{
    
    
			index *= -1;
		}
		else
		{
    
    
			index = 1;
		}
		str++;
	}

	if ((*str == '0') && (*(str + 1) == 'x' || *(str + 1) == 'X'))      //判断前两位是否是0X 16进制的表达形式
	{
    
    
		str += 2;     //如果是从第三位开始换算计算
	}
	else
	{
    
    
		return INT_MAX;
	}

	while (isxdigit(*str))        //判断是否是字符
	{
    
    
		if (isdigit(*str))
		{
    
    
			sum = sum * 16 + (*str - '0');
		}
		else if (islower(*str))
		{
    
    
			sum = sum * 16 + (*str - 'a' + 10);
		}
		else
		{
    
    
			sum = sum * 16 + (*str - 'A' + 10);
		}
		str++;
	}
	if (*str == '\0')
		return sum * index;
	return INT_MAX;
}

int main()
{
    
    
	printf("%d\n", Get_0x("0x123ABcW"));
	printf("%d\n", Get_0x("0X123ABc"));
	printf("%d\n", Get_0x("      0x123ABc"));//对空格判断
	printf("%d\n", Get_0x("+0x123ABc"));//+判断
	printf("%d\n", Get_0x("-0x123ABc"));//-判断
	printf("%d\n", Get_0x("  +0x123ABc"));//+和空格判断
	printf("%d\n", Get_0x("   -0x123ABc"));//-和空格判断
	printf("%d\n", Get_0x("0x2AF5"));//对上述举例进行验证
}

Validation results

insert image description here

(A Xiaobai, if you have any questions, you are welcome to correct me)

Guess you like

Origin blog.csdn.net/weixin_56935264/article/details/121316969