cancel
Showing results for 
Search instead for 
Did you mean: 

Keyboard widget, how to convert Unicode char* from to char*

NGune.1
Associate II

Hi,

I am trying to use keyboard widget in TouchGfx 4.21. I have global

char *name;

variable. After a lot of trial, I couldn't find a way to copy the value returned by keyboard.getBuffer() to my global variable. I even tried to convert to UTF-8 first, but no success.

Briefly, I just want to store the value returned by keyboard.getBuffer() to my global variable.

Can somebody help please?

Thanks

5 REPLIES 5
Yoann KLEIN
ST Employee

Hello @NGune.1​,

Did you already try with the strncpy() method ?

Otherwise, with simply casting the UnicodeChar* to char*, like : (char*)UnicodeChar*.

/Yoann

Yoann KLEIN
ST Software Developer | TouchGFX
NGune.1
Associate II

Hi Yoann,

Thanks for your reply.

With this:

strncpy(*name, keyboard.getBuffer(), 11);

I get :

error: cannot convert 'touchgfx::Unicode::UnicodeChar*' {aka 'short unsigned int*'} to 'const char*'

If I first convert to utf-8 :

Unicode::toUTF8(keyboard.getBuffer(), buff, 11);
strncpy(*name, buff, 11);

I get:

error: invalid conversion from 'uint8_t*' {aka 'unsigned char*'} to 'const char*' [-fpermissive]

if "buff" is defined as uint8_t *buff;

and I get:

error: invalid conversion from 'char*' to 'uint8_t*' {aka 'unsigned char*'} [-fpermissive]

if "buff" is defined as char *buff;

Yoann KLEIN
ST Employee

Hi,

I just did that this cast between a UnicodeChar* and a char* and it seems to work, at least it does not trigger any compilation issues :

touchgfx::Unicode::UnicodeChar* unicodeChar = textArea1Buffer;
char* test = (char*)unicodeChar;

Could you try to reproduce that in your project ?

/Yoann

Yoann KLEIN
ST Software Developer | TouchGFX

By the way, strncpy() is not usable for normal string copying. Read what it actually does:

https://en.cppreference.com/w/c/string/byte/strncpy

If count is reached before the entire array src was copied, the resulting character array is not null-terminated.

If, after copying the terminating null character from src, count is not reached, additional null characters are written to dest until the total of count characters have been written.

NGune.1
Associate II

Hi Yoann,

Thank you for your suggestion. Yes, it took me one step further. I mean, with your solution I didn't get any compiler error. However, in the end, the variable 'test' contains only the first entry, although I can see the entered text is entirely buffered.

I tried both:

char* test = (char*)keyboard.getBuffer();

and

char* test;
strncpy(test, (char*)keyboard.getBuffer(), 11);

but the result is the same.

Could you please check the attached screenshot?

There are two buffers. I marked them as 1 and 2 in the picture. I entered "ABCD" using the touch keyboard. In the first buffer there is only "A", but the second one contains "ABCD". I think, for some reason keyboard.getBuffer() returns only the first entry, or I am doing something wrong.

What may be the reason for this situation?

Thanks,

0693W00000Y9ejoQAB.png