[C Language·pta]--Convert long integer to hexadecimal string

This article mainly explains two methods

Table of contents

Law 1. Violent conversion

Method 2, library function method--sprintf


This question requires the implementation of a simple function that converts a long integer into a hexadecimal string.

Function interface definition:
void f( long int x, char *p);
wherex is the decimal long integer to be converted , p< a i=8> points to the first element of a character array. The function of functionf is to convert the converted hexadecimal string Writes the array pointed to by p. A~F in hexadecimal are capital letters.

Sample Referee Test Procedure:

#include <stdio.h>
#define MAXN 10

void f( long int x, char *p );

int main()
{
    long int x;
    char s[MAXN] = "";
    
    scanf("%ld", &x);
    f(x, s);
    printf("%s\n", s);
    
    return 0;
}

/* 你的代码将被嵌在这里 */


Input example 1:
123456789
Exit example 1:
75BCD15
Example 2 for import:
-125
Example 2 for import:
-7D

First, you need to understand how decimal is converted to hexadecimal:

The conversion from decimal to any base is The integer part: divide by the base (for example, the base of octal system is 8, the base of hexadecimal system is 16) and the remainder is obtained ( the remaining remainder). Decimal part: Multiply the base number to get the integer part (a positive integer), until the decimal is 0. If it is a loop, it will be discussed separately.

 After you know how to convert decimal to hexadecimal, you can use code ideas to achieve the conversion. Because this question does not involve converting the decimal fraction into hexadecimal, so only You need to consider how to convert the integer part into hexadecimal.

Law 1. Violent conversion

Key steps: ①. Find the remainder and write it down ②. Convert the remainder written down into the corresponding uppercase letters in hexadecimal

Small issues that need to be considered:①. What to do when the number is negative ②. How to write down the remainder

Idea:

①. Determine whether it is a negative number. If so, add a sign.

②. First get a remainder of x%16 and convert it to the corresponding hexadecimal character.

③. Count down the remainder obtained

Detailed explanations are in the code:

#include <stdio.h>
#define MAXN 10

void f(long int x, char* p)
{
    int i = 0;
    while (x)
    {//一、如果这个数是负数
        if (x < 0)
        {//第一个元素置为-
         //负数再变为正数,方便进行下面的转换操作
            p[i++] = '-';
            x = -x;
        }
        p[i] = x % 16;
    //二、10进制转16进制
        if (p[i] > 9)//如果得出的16进制的数>9就要考虑转化为字母的情况
        {//16进制范围:0~9,A(16进制代表10,它对应ASC码为65,所以得出的16进制数+55,就可以转换为对应的字母) B(11) C(12) D(13) E(14) F(15)
         //我们算出来的是数字,而我们要存入字符数组(有可能是16进制有字母的原因),所以要从数字转换为字符
            p[i] += 55;//大于9就转换为对应的大写字母
            i++;
        }
        else
        {
            p[i] += 48;//<=9就应把对应的数字转换为字符,因为字符0~9的ASC码对应为48~57,所以数字转换为字符就+48即可
            i++;
        }
        x /= 16;
    }
    p[i] = '\0';
    //三、逆置字符数组,因为10进制转16进制,对应余数是倒着数的
    int left = (p[0] == '-') ? 1 : 0, right = i - 1;//负数的话就要从下一个下标为left倒置
    while (left < right)
    {
        char temp = p[left];
        p[left] = p[right];
        p[right] = temp;
        left++;
        right--;
    }
}

int main()
{
    long int x;
    char s[MAXN] = "";

    scanf("%ld", &x);
    f(x, s);
    printf("%s\n", s);

    return 0;
}

Method 2, library function method--sprintf

First review sprintf

Function prototype

int sprintf(char* buffer,const char* format[,argument] ...);

The first parameter: the string to be written

The second parameter: what format needs to be printed, %d, %f, etc. (formatted string)

The third parameter: variable

Function function:Write formatted data into a string

example:

①. Print the integer 123 into a string and save it in s

sprintf(s,"%d",123);-----then the character 123 is placed in the s character array

printf("%s",s);------------- prints character 123

Let’s learn more about %x:

%x can only be used to output unsigned integers and pointer type variables. If used to output signed integers, the result may be wrong.

So if the number is a negative number (also called a signed number), it must be converted to unsigned first, and then %x can be used

code show as below:

void f(long int x, char* p)
{
	if (x < 0)
	{
		sprintf(p, "-%X", -x);
//先把数转换为正数,这样才能被%x正确转化
//然后转换为16进制,写入字符数组p中
//其中%x前面加个-,就会把转化后的16进制数从整数转化为负数了
	}
	else
	{
		sprintf(p, "%X", x);
	}
}

Guess you like

Origin blog.csdn.net/m0_74044018/article/details/130504534