Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare multibyte character array where bytes > 2

How can I declare a multibyte character array in which each is character is represented for 3 or 4 bytes?

I know I can do: char var[] = "AA"; which will write to memory 6161 and I can do wchar var[] = L"AA"; which will do 00610061. How can I declare a wider character array in C or C++?

Is there any other prefix like the L to instruct the compiler to do so?

like image 548
user1618465 Avatar asked Apr 26 '26 20:04

user1618465


2 Answers

Both C and C++ offer char32_t. In C char32_t is a typedef of/same type as uint_least32_t. In C++ char32_t has the same size, signedness, and alignment as std::uint_least32_t, but is a distinct type.

Both of them can be used like

char32_t string[] = U"some text";
like image 62
NathanOliver Avatar answered Apr 28 '26 09:04

NathanOliver


You could try this, as long as you don't mind manually typing out each character:

int characters[3] = { 'h', 'e', 'y' };

You can also use a capital U in front of the string literal to get UTF-32:

char32_t characters[] = U"hey";
like image 37
Josh Karns Avatar answered Apr 28 '26 10:04

Josh Karns