BIM Coordinator Program (INT) April 22, 2024

Find the next step in your career as a Graphisoft Certified BIM Coordinator!

Archicad C++ API
About Archicad add-on development using the C++ API.

How to export utf-8 strings?

Andor Kocsis
Contributor
Code to test (AC23):
char orig_utf8[8];
orig_utf8[0] = 0x6b;
orig_utf8[1] = 0xc3;
orig_utf8[2] = 0xa1;
orig_utf8[3] = 0x74;
orig_utf8[4] = 0x79;
orig_utf8[5] = 0xc3;
orig_utf8[6] = 0xba;
orig_utf8[7] = 0x00;

GS::UniString gs_str_in(orig_utf8, CC_UTF8);
ACAPI_WriteReport(gs_str_in, true);  //reports "kátyú", this is the real utf8 string

GS::UChar c1, c2, c3, c4, c5;
c1 = gs_str_in.GetChar(0);
c2 = gs_str_in.GetChar(1);
c3 = gs_str_in.GetChar(2);
c4 = gs_str_in.GetChar(3);
c5 = gs_str_in.GetChar(4);

GS::UniString result("");
result += c1;
result += c2;
result += c3;
result += c4;
result += c5;
ACAPI_WriteReport(result, true);  //reports "kktyy" - why?
Outputs: Why does it report "kktyy"?

How to get utf char array out of a GS::UniString without corruption and memory leak?

Thx,
1 REPLY 1
Viktor Kovacs
Graphisoft
Graphisoft
To get a char array with UTF-8 encoding you should use the ToCStr function of GS::UniString.

For example:

str.ToCStr (0, MaxUSize, CC_UTF8)).Get ();

Be careful though, the intermediate CStr object will be disposed after the call, so you have to use the result right in the operation.

For example converting to a wstring can be done like this:

std::wstring ToWString (const GS::UniString& str)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
	return std::wstring (convert.from_bytes (str.ToCStr (0, MaxUSize, CC_UTF8)));
}
Learn and get certified!

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!