Cannot convert 'const char*' to 'unsigned char*

I am trying to convert string into unsigned char array but it gives me an error that I dont get from GCC compiler on VS code. It wont take unsigned char as a input parameter for my function. I think there is an issue with the compiler for ESP32, but I do not know how to access or change that. Would really love some help on this.
(Even just an arduino function that converts strings into unsigned char array)

void str_hex(unsigned char text[]){
    int i, j;
    unsigned char strH[sizeof(text)/sizeof(text[0])];
    memset(strH, 0, sizeof(strH));

    /*converting str character into Hex and adding into strH*/
    for (i = 0, j = 0; i < (sizeof(text)/sizeof(text[0])); i++, j += 2) {
        sprintf((char*)strH + j, "%02X", text[i]);
    }
    strH[j] = '\0'; /*adding NULL in the end*/
}


void setup() {
  Serial.begin(115200);

}

void loop() {
  str_hex("1234");
  delay(10000);

}

cannot convert 'const char*' to 'unsigned char******

Why are you using unsigned char at all? Just use const char*

void str_hex(const char* text)

The compiler is perfectly fine - you should not convert something that is constant into something that is not. Risky business

Note that your function is using a local variable strH for the target buffer so it’s gone when you exit the function and sizeof won’t do what you need with a pointer (the size will be 2 or 4, not the length of the string - use strlen() if it’s a cString you pass)

What is this function supposed to do with « 1234 »?

Well its supposed to convert it to hex using that %02X format specifier. Do you know of a better way to to this. I've been googling for hours. I need to convert string into hex array to use it for serial communication.

Do you mean that the string "1234" needs to become the number 0x1234?

use strtol() or strtoul() for example (or strtoull() if you need an unsigned long long)

PS/ please never post images of text... it's totally unusable and just accelerates climate change... just post text using code tags when an image is not necessary

Apologise about that. its to convert "1234" to "31 32 33 34". Converting each character in the string to its coresponding hex value.
And if I use const char as my input parameter, it does not correctly convert it to hex :frowning:

ah OK - so you want to see the ASCII code in HEX for the input string. May be something like this?

void asciiCodeFor(const char * str) {
  while (*str) {
    if (*str < 0x10) Serial.write('0');
    Serial.print((byte) *str, HEX);
    Serial.write(' ');
    str++;
  }
}

void setup() {
  Serial.begin(115200); Serial.println();
  asciiCodeFor("1234");
}

void loop() {}
1 Like

consider (corrected)

 31 32 33 34
 37 38 39
char *
str_hex (
    const char * text)
{
    static char s [80];
    char t [10];

    s [0] = '\0';
    for (unsigned i = 0, j = 0; i < strlen(text); i++, j += 2) {
        sprintf (t, " %02X", text[i]);
        strcat (s, t);
    }
    return s;
}

void setup() {
    Serial.begin (115200);
    Serial.println (str_hex ("1234"));
    Serial.println (str_hex ("789"));
}

void loop() {
}
1 Like

Thank you so much, I love you. This is exactly what I needed. Its surprisingly difficult to make this into a function that works work with ESP32 inbuilt C includes. You got it first try, and I see where I went wrong :man_facepalming:.

if you just want to print the result, there is no real need to create an extra buffer, just print as you go through the data

1 Like

Thats true, but in my case I need to use srial.write to transmit data. For that I'd need to first put it in an array and send the array all at once.

But if I need to dynamically allocate the size of the HEX array based on the input array, how could I do that?

Is it possible to make the static char array into a dynamic char array based on input text?

I don't think so. Serial transmission is really slow compared to processor compute speed. If you did as @J-M-L suggested, you would not be able to tell the difference by looking at the stream of serial data. It would also look "all at once".

I should specify that its communication between two ESP's and a display. For the diplay, the only way to send values is by specifing memory adress and data. The data technically doesnt have to go all at once but it seems more efficient for a single instruction to send everything.

i tried to correct what you posted.

a common approach is for the calling function to provide a buffer to put the resulting string in along with the size of the buffer.

an approach using a buffer rather than simply "outputting" the result is useful when it may be output to multiple devices such as a display device on the target (e.g. OLED) and the Serial I/F for debugging

Are you sure of that?? On a serial line it never goes all at once…

Also are you sure you need to send the asci codes in ascii ?

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