cancel
Showing results for 
Search instead for 
Did you mean: 

Unicode::snprintf string problem

Tuoman
Senior II

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PBull
Associate III

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);

View solution in original post

3 REPLIES 3
PBull
Associate III

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);

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.

also, work

const uint8_t str[] = "test";
Unicode::fromUTF8(str, textAreaItemBuffer, 20);
textAreaItem.setWildcard(textAreaItemBuffer);

😊