Converting byte array received from LoRa Module to a single int/float value

I am building a project where I transmit sensor data to its receiver via LoRa E5 module. After receiving, I plan it to be converted into an int/float value so that it can be stored to its corresponding variable. Upon using the function:

static void recv_prase(char *p_msg)
{
    if (p_msg == NULL)
    {
        Serial.println("Received null");
        return;
    }

    char *p_start = NULL;
    char data[128];       // To hold the received bytes as characters
    int bytes_len = 0;

    p_start = strstr(p_msg, "RX"); // Find the "RX" keyword in the message
    if (p_start && (1 == sscanf(p_start, "RX \"%127[^\"]\"", data)))
    {
        // Calculate the length of the hex string
        bytes_len = strlen(data);
        Serial.print("Data received as hex string: ");
        Serial.println(data);

        // Convert the hex string to an integer
        int result = 0;
        sscanf(data, "%X", &result);

        // Print the received bytes (optional, to show the bytes)
        Serial.print("Received Bytes: ");
        for (int i = 0; i < bytes_len / 2; i++)
        {
            Serial.print((result >> (8 * (bytes_len / 2 - 1 - i))) & 0xFF, HEX);
            Serial.print(" ");
        }
        Serial.println();

        // Print the converted int value directly as the hex string interpreted
        Serial.print("Converted Int: ");
        Serial.println(result);
    }
}

+TEST: LEN:3, RSSI:-48, SNR:6

+TEST: RX "101203"

Data received as hex string: 101203

Received Bytes: 10 12 3

Converted Int: 1053187

This is the output I get.

I wish to have my Converted Int the same with the string after RX.

I have some hypotheses as to why this problem occurred but I need advises from the people in the community who is/are more knowledgeable in the field and I'm not sure if I am right.

char arr[]={10,12,3};
long Integer=0;
for(int i=0;i<sizeof(arr);i++)Integer *= 100 +arr[i];
Serial.println(Integer);

Why is this being considered a "hex string"? Please post the code that sends this message.

If the value in quotes is ASCII text, and is supposed to represent a decimal value, use a function like atol() to convert the character string to a long integer.

I've updated that code, sorry for that. The LoRa Module sends data as a hex string and here the data is received and stored in a char array. And it seems the integer data that I sent in the transmitter is taken as a literal hex, since the data was sent as a hex string. Which is why the reason my converted int produces the output not the same with the integer value that I sent. My problem now is how to convert my integer data to hex so that when it is decoded in the receiver end, it should produce the output similar to what integer values I intend to send.

Sorry, that makes no sense, as data are always stored and transmitted as binary. Post the actual transmitter code, with an example of what is being transmitted.

"Hex" is a human readable representation of binary data.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.