Convert Godot's String to C++'s std::string

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By markwiering

Hello there!

With Godot, when using C++ modules, how do I convert Godot’s String to C++'s std::string ?

I found this page here ( https://forum.godotengine.org/110221/c-string-to-string ) explaining how to convert std::string to Godot’s String, but not a page that explains how to do the opposite.

So… if anybody knows… how do I convert Godot’s String to std::string ?

:bust_in_silhouette: Reply From: Zion

For Godot 3.3, could likely use unicode_str(). Reference:

const wchar_t *String::unicode_str() const {
	return godot::api->godot_string_wide_str(&_godot_string);
}

For Godot 4.0, I made the conversion using utf().get_data() in the following sample:

String foo = "helloworld";  // Godot's String
std::string stdstring_foo(foo.utf8().get_data());
std::cout << "stdstring_foo: " << stdstring_foo << std::endl;
// outputs:
// stdstring_foo: helloworld

Reference:

Or could like use ascii().get_data() too: