c++ string to String

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

Hey Guys,

I’m trying to get a string to a String for a GDnative script so I can pass it through to another nodes function.
Really I just want the Int of the utf-8 as i’m just send 8 bit numbers across the serial link.
I think the reason I’m getting blank Strings sometimes is to do with Strings being UTF-32 (could be wrong).

Does anyone know the correct way to convert from a c++ string to String?
This is what I’m trying:

	Godot::print("Port message received");	
	string msg = my_serial.read(1);
	String godot_msg = msg.c_str();
	Godot::print("Message: " + godot_msg);
:bust_in_silhouette: Reply From: sash-rc

Basically, yes: godot::String accepts const char*, which is returned by std::string::c_str(), as well as wchar_t*, so this part looks ok.

For empty strings, you’d better check that after this line string msg = my_serial.read(1); with debugger breakpoint or if (msg.empty())

So I change it to this

	Godot::print("Port message received");	
	string msg = my_serial.read(1);
	if (msg.empty()) 
	{
		Godot::print("Message Empty");

	}
	else
	{
		Godot::print("Message:" + String(msg.c_str()));
	}

I get in the terminal:

Port message received
Unicode error: invalid skip
Message:
Unicode error: invalid skip
Message:
Port message received
Message::heart:
Port message received
Message:
Port message received
Message::smiling_face:
Port message received
Message:
Port message received
Message:
Port message received
Message:
Port message received
Message::slight_smile:

Raging_Panda | 2021-07-15 01:53