C implementation of integer and string conversion

Publisher:光子梦境Latest update time:2015-05-06 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I am about to find a job, so I reviewed the relevant knowledge and looked at the written test questions of embedded C. Overall, I feel that the test questions are quite interesting. The test points are relatively important and detailed, and they mainly involve some basic concepts such as pointers, arrays, and function pointers.

For example:
How to achieve the maximum value of two values: ((a+b) + abs(ab))/2
How to achieve the exchange of two variable values ​​without relying on intermediate quantities:
a = a + b;
b = a - b;
a = a - b;
or
a = a^b;
b = a^b;
a = a^b;
The latter method is more superior.
 
The difference between sizeof and strlen
Sizeof is an operator, and the result can be determined during the compilation process, but strlen is a function, and the return value can only be obtained during the run time.
 
The combination of pointers in various forms such as pointers, arrays, and functions is also the focus of the questions.
 
In programming, the main design involves the processing of strings and linked lists. I will find a few simple programs as training. Implement the conversion problem between integers and strings. This conversion problem is essentially to handle the switching relationship between ASCII codes and numbers, that is, the ASCII code form of numbers is '0'= 0 + 48; '9'= 9 + 48; 0 = '0'-48.
This relationship is the most important issue in the conversion process. Of course, when converting integers to strings, we need to pay attention to the impact of negative numbers. If negative numbers are not handled correctly, some inexplicable results may occur.
 
First, implement the conversion process from string to integer: This process is relatively easy, because this process can directly determine whether it is a positive number or a negative number. Just determine the content corresponding to the subscript 0. The rest can be achieved by gradually summing and accumulating. The basic conversion process is as follows:

 

    int myatoi(char *src)
    {
        char flag = 0;
        int sum = 0;
        int i = 0;
        int len ​​= strlen(src);
        
        /*Parameter correctness*/
        if(NULL == src)
        {
            return 0;
        }
        
        /*Is there a sign problem*/
        if(src[i] == '-')    
        {
            flag = '-';
            ++ i ;
        }

        for( ; i < len ; ++ i)
        {
            /*Judge whether the character is legal*/
            if(src[i] < 48 && src[i] > 57)
                return 0;
            
            /*Sum the data, pay attention to the conversion of the value src[i] - 48*/
            sum = sum *10 + src[i] - 48;
        }
        
        /*Return the correct positive and negative number according to the flag bit*/
        if(flag == '-')
            return -sum;
        else
            return sum;
    }

Integer to string conversion: Actually, it is to separate the bits of an integer and then sort the string. Because the uncertainty of the length of the number makes it difficult to quickly determine the length of the string, it can only be decomposed first and then sorted. At the same time, it is also necessary to pay attention to the existence of symbols. The basic conversion process is as follows:

 

    char* myitoa(int num, char *str)
    {
        char flag = 0;
        int i = 0, count = 0, j = 0;

        /*Parameter detection*/
        if(NULL == str)
        {
            return NULL;
        }
        
        /*Judge the positive or negative value and set the corresponding sign*/
        if(num < 0)
        {
            str[i ++] = '-';

            /************************
            At the same time, take the absolute value of the value
            to ensure that the subsequent remainder division operation is normal
            ****************************/
            num = -num;
        }
        
        while(num / 10)
        {
            /***************************
            count is used to save the actual number of digits,
            which is convenient for later sequence exchange
            ****************************/
            ++ count;
            /*The actual saved order is the reverse sequence*/
            str[i++] = num % 10 + 48;
            num /= 10;
        }
        /*The judgment is*/
        if(num %10)
        {
            str[i++] = num %10 + 48;
            ++ count;
        }
        /*String terminator*/
        str[i] = 0;
        
        /*Achieve better data operation*/
        i = 0;
        if(str[0] == '-')
        {
            i = 1;
        }
        /*The actual subscript interval for exchange is i~j*/
        j = count + i - 1;

        for(; i < j ; ++ i, --j)
        {
            /* Better operation of data*/
            str[i] = str[i] + str[j];
            str[j] = str[i] - str[j];
            str[i] = str[i] - str[j];
        }

        /*Return the actual string*/
        return str;
    }

The above code implements the conversion problem between strings and integers, and basically realizes conversion in various situations.

Reference address:C implementation of integer and string conversion

Previous article:Analysis of memory space size (sizeof) of classes in C++
Next article:Analysis of strings and character arrays in C language

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号