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

Convert from GS::UniString to std::string

dushyant
Enthusiast

Hi

How to convert from GS::UniString to std::string?

(I need to pass it to another API which takes std::string)

Thanks.

3 REPLIES 3
Viktor Kovacs
Graphisoft
Graphisoft

To convert between GS::UniString and std::wstring:

GS::UniString ToUniString (const std::wstring& str)
{
	static_assert (sizeof (GS::UniChar::Layout) == sizeof (wchar_t), "string character size mismatch");
	return GS::UniString ((const GS::UniChar::Layout*) str.c_str (), (GS::USize) str.size ());
}

std::wstring ToWString (const GS::UniString& str)
{
	static_assert (sizeof (GS::UniChar::Layout) == sizeof (wchar_t), "string character size mismatch");
	return std::wstring ((const wchar_t*) str.ToUStr ().Get (), str.GetLength ());
}

To convert between std::string and std::wstring:

std::wstring StringToWString (const std::string& str)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
	return std::wstring (convert.from_bytes (str));
}

std::string WStringToString (const std::wstring& str)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
	return std::string (convert.to_bytes (str));
}
dushyant
Enthusiast

Hi Viktor,

Thanks for that info.

I found that simply doing:  std::string myStdStr(myUniStr.ToCStr())  works.

drjustice
Newcomer

 

(removed because redundant suggestion)