Skip to main content
Wrend.1
Associate III
September 25, 2020
Question

How deal with "wchar_t" in textArea?

  • September 25, 2020
  • 2 replies
  • 1048 views

I want to display a long "wchar_t" type text in a textArea.

Here is my code.

void seasoningDisplay::setSeasoningText(const wchar_t* input)
{
	touchgfx::Unicode::UnicodeChar tmpfmt[200];
	touchgfx::Unicode::strncpy(tmpfmt, (const char*)input, 200);
	Unicode::snprintf(seasoningTextBuffer, SEASONINGTEXT_SIZE, "%s", tmpfmt);
	seasoningText.resizeToCurrentText();
	seasoningText.invalidate();
}

I know it is not correct, please tell me how to realize it.

This topic has been closed for replies.

2 replies

Martin KJELDSEN
Principal III
September 28, 2020

What\re you putting into `input`? UnicodeChar is 16-bit unsigned int, so just make sure your conversion matches that.

Wrend.1
Wrend.1Author
Associate III
September 29, 2020

Yes. I use this function like:

setSeasoningText(L"这是中文");

​I used to think I need to use "touchgfx:: Unicode:: strncpy (tmpfmt, (const char *) input, 200) " to break through the textarea display size (63). It is my foolly.

Now I write the function like this:

void seasoningDisplay::setSeasoningText(const char* input)
{
	setlocale(LC_ALL, "chs");
	wchar_t *m_wchar = NULL;	
	int len = mbstowcs(m_wchar, input, 100) + 1;
	m_wchar = new wchar_t[len];
	mbstowcs(m_wchar, (const char*)input, 100);
	
	Unicode::snprintf(seasoningTextBuffer, SEASONINGTEXT_SIZE, "%s", m_wchar);
	seasoningText.resizeToCurrentText();
	seasoningText.invalidate();
	
	memset(m_wchar,0, len);
	delete m_wchar;
	setlocale(LC_ALL, "C");
}

By the way, It failed only in real board (compiled by keil). I works in PC simulator.

It just work when I use the char* like"12345". When I use the chinese character. I failed due to the exception of "mbstowcs".

So, how to setup keil make it work?