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.
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
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 .
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.
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