How to convert byte array (containing hex values) to decimal

前端 未结 5 909
时光说笑
时光说笑 2021-01-29 08:02

I am writing some code for an Atmel micro-controller. I am getting some data via Uart, and I store these hex values into an array.

Suppose the elements of the array are:

5条回答
  •  北海茫月
    2021-01-29 08:43

    If data is declared as stated in the comments (char data[]={0x1F, 0x29, 0x3C}), you can run this program.

    #include 
    #include 
    int main()
    {
        char receivedByte[9], *p;
        char data[] = { 0x1F, 0x29, 0x3C };
        sprintf(receivedByte, "0x%X%X%X", data[0], data[1], data[2]);
        int intNumber = strtol(receivedByte, &p, 16);
        printf("The received number is: %ld.\n", intNumber);
        return 0;
    }
    

提交回复
热议问题