简体   繁体   中英

Converting a HEX value to DECimal value in C

I have tried out many ideas from SO. One of them worked (output was DEC 49 for HEX 31) when I tested it in here onlinegdb . But, when I implemented it in my application, it didn't produce same results. (output was 31 for 31 again).

The idea is to use a string value (full of HEX pairs); An example; 313030311b5b324a1b5b324a534f495f303032371b

I need to convert each integer pair (HEX) into equivalence decimal value. eg

HEX => DEC
31  => 49
30  => 48

I will then send the DEC value using UART value by value.

The code I test the behavior is below and here ; But, it doesn't have to be that code, I am open to all suggestions as long as it does the job.

#include <stdio.h>
    
int isHexaDigit(char p) {
    return (( '0' <= p && p <= '9' ) || ( 'A' <= p && p <= 'F'));
}
    
int main(int argc, char** argv) 
{ 
    char * str = "31";
    char t[]="31";
    char* p = t;
    char val[3]; // 2 hexa digit 
    val[2] = 0;  //and the final \0 for a string
    int number; 
        
    while (isHexaDigit(*p) && isHexaDigit(*(p+1))) {
        val[0] = *p;
        val[1] = *(p+1);
    
        sscanf(val,"%X", &number);    // <---- Read hexa string into number
        printf("\nNum=%i",number);    // <---- Display number to decimal.
                  
        p++;
        //p++;
        if (!*p) break;
        p++;
    }
    return 0; 
} 

EDIT I minimized the code. Odd-length string is ignored for the time being. The code sends out the data byte by byte. In the terminal application, I get the values as HEX, eg HEX 31 instead of DEC 49. They are actually same. But, a device I use requires DEC 49 version of the value (which is ASCII = 1)

在此处输入图像描述

Any pointer highly appreciated.

You can use strtol function to convert your hex string to binary and then convert it to a decimal string in a single line:

snprintf(str_dec, 4, "%ld", strtol(str_hex, NULL, 16));

Your code becomes:

#include <stdio.h>
#include <stdlib.h>

int isHexaDigit(char p) {
    return (( '0' <= p && p <= '9' ) || ( 'A' <= p && p <= 'F'));
}

int main(int argc, char** argv)
{
    char * str = "31";
    char t[]="31";
    char* p = t;

    char str_hex[3] = {0,};
    char str_dec[4] = {0,};

    while (isHexaDigit(*p) && isHexaDigit(*(p+1))) {

        str_hex[0] = *p;
        str_hex[1] = *(p+1);

        /* Convert hex string to decimal string */
        snprintf(str_dec, 4, "%ld", strtol(str_hex, NULL, 16));

        printf("str_dec = %s\n", str_dec);

        /* Send the decimal string over UART1 */
        if (str_dec[0]) UART1_Write(str_dec[0]);
        if (str_dec[1]) UART1_Write(str_dec[1]);
        if (str_dec[2]) UART1_Write(str_dec[2]);

        /* Reset str_dec variable */
        str_dec[0] = 0;
        str_dec[1] = 0;
        str_dec[2] = 0;

        p++;
        if (!*p) break;
        p++;
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM