2023-01-29 12:19 PM
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
2023-01-30 12:32 AM
Hello @NGune.1,
Did you already try with the strncpy() method ?
Otherwise, with simply casting the UnicodeChar* to char*, like : (char*)UnicodeChar*.
/Yoann
2023-01-30 02:55 PM
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;
2023-01-31 07:36 AM
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
2023-01-31 02:54 PM
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.
2023-01-31 05:19 PM
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,