String to CString
Monkey Forums/Monkey Beginners/String to CString| 
 | ||
| Hi all I need to convert a Monkey String to normal CString (char*) Any idea, how i can do that? My start was: char *StringToChar(String str)
{
	char *c = str.ToCString<char>();
	return "";
}but i gives me an error:  main.cpp: In function 'char* StringToChar(String)': main.cpp:2213:31: error: invalid user-defined conversion from 'String::CString<char>' to 'char' [-fpermissive] char c = str.ToCString<char>(); can anybody help me? | 
| 
 | ||
| Monkey uses wide char (Unicode) characters and strings. functions.cpp const Char* StringToChar(String str) {
    return str.Data();
}
const Char* StringToCharZ(String str) {
    return std::wstring(str.Data(),str.Length()).c_str();
}
String NullTerminatedString(String str) {
    Char character=0;
    return str + String(&character,1);
}
std::wstring StringToWString(String str) {
    return std::wstring(str.Data(),str.Length());
}
String::CString<Char> StringToCString(String str) {
    return str.ToCString<Char>();
}ToCString.monkey Import "functions.cpp"
Extern
    Function StringToChar(str:String)
    Function StringToWString(str:String)
    Function StringToCString(str:String)
    Function NullTerminatedString:String(str:String)
Public
Function Main:Int()
    StringToChar("myString")
    StringToWString("myString 2")
    StringToCString("myString 3")
    
    Local str:= NullTerminatedString("Hello World!")
    For Local i:= 0 Until str.Length()
        Print Int(str[i])
    Next
    Return 0
End | 
| 
 | ||
| thanks for this hint.... in my case, it'ss pointless to use "String::CString<Char>" right? It's monkey only? | 
| 
 | ||
| If you want C++ strings in C++ functions, I think you would use std::wstring(str.Data(),str.Length()) | 
| 
 | ||
| when i use str.Data(), then it seems like, it have none NULL Terminated end... how can i fix that? if (this->hWnd != 0) SetWindowTextW(this->hWnd, /*this->text.ToCString<char>()*/this->text.Data()); | 
| 
 | ||
| Try if (this->hWnd != 0) SetWindowTextW(this->hWnd, std::wstring(this->text.Data(),this->text.Length()).c_str()); or const Char* StringToCharZ(String str) {
    return std::wstring(str.Data(),str.Length()).c_str();
}
if (this->hWnd != 0)
    SetWindowTextW( this->hWnd, StringToCharZ(this->text) );or String NullTerminatedString(String str) {
    Char character=0;
    return str + String(&character,1);
}
if (this->hWnd != 0)
    SetWindowTextW( this->hWnd, NullTerminatedString(this->text).Data() );Better use NullTerminatedString() directly when you get the string from Monkey code, because WinAPI always wants Null-Terminated strings. Also edited first answer to show NullTerminatedString() in monkey code. | 
| 
 | ||
| Ahhh thanks for the code. I was hoping, thats easier :D | 
| 
 | ||
| If you define this->text as being of type std::wstring, you would write API functions like this: if (this->hWnd != 0)
    SetWindowTextW( this->hWnd, this->text.c_str() ); |