‎2020-03-20 03:21 AM
Hi,
I cannot seem to be able to print char array to unicode buffer.
I have set Wildcard ranges to 0-9,A-z (also tried 0x20-0x7E)
I'm trying to display file names in the UI, which have arbitrary and modifyable names.
When I do snprintf with "%s" with a char array, TouchGFX will print ?-marks, character values in the buffer appear to be of values around 25 000. Do I need to convert characters somehow?
Why this results putting numbers ~25000 to the buffer, and thus ?-marks intead of "test"?
char text[] = "test"; //allocate global variable
...
Unicode::snprintf(textAreaItemBuffer, TEXTAREAITEM_SIZE, "%s", text);
However, this works fine
Unicode::snprintf(textAreaItemBuffer, TEXTAREAITEM_SIZE, "%s", touchgfx::TypedText(T_TEST).getText());
And printing numbers using %d also works.
What is wrong with snprinting char array with "%s"?
Thanks!
Solved! Go to Solution.
‎2020-03-23 12:03 PM
Hi,
your code doesn't work because %s needs a zero-terminated UnicodeChar list, but you passed a const char ("test").
getText() also returns a Unicode::UnicodeChar*, so it works.
You have to copy your char array to a UnicodeChar list and pass your UnicodeChar buffer to snprintf:
Unicode::UnicodeChar buffer[20];
const char str[] = "test";
Unicode::strncpy(buffer, str, 20);
Unicode::snprintf(textAreaItemBuffer, 20, "%s", buffer);
‎2020-03-23 12:03 PM
Hi,
your code doesn't work because %s needs a zero-terminated UnicodeChar list, but you passed a const char ("test").
getText() also returns a Unicode::UnicodeChar*, so it works.
You have to copy your char array to a UnicodeChar list and pass your UnicodeChar buffer to snprintf:
Unicode::UnicodeChar buffer[20];
const char str[] = "test";
Unicode::strncpy(buffer, str, 20);
Unicode::snprintf(textAreaItemBuffer, 20, "%s", buffer);
‎2020-03-24 04:06 AM
Thank you a lot, works perfectly! Saved me full day of trying to make this work.
I tried similar approach, but for some reason this does not work:
Unicode::UnicodeChar buffer[20];
const char str[] = "test";
Unicode::snprintf(buffer, 20, "%s", str);
Unicode::snprintf(textAreaItemBuffer, 20, "%s", buffer);
I always have used snprintf instead of strncpy to ensure null termination. I thought this would be interchangeable.
‎2022-05-20 05:10 AM
also, work
const uint8_t str[] = "test";
Unicode::fromUTF8(str, textAreaItemBuffer, 20);
textAreaItem.setWildcard(textAreaItemBuffer);
:smiling_face_with_smiling_eyes:
‎2024-04-29 03:04 AM - edited ‎2024-04-29 03:07 AM
Hello all,
For an easier way to print string, refer to that thread Solved: TouchGFX 4.21.1; Unicode; Wildecard; Simulator wor... - STMicroelectronics Community
Regards,