I have a standard char pointer which im trying to cast to a string.
// string to char*
char *x = cast(char*)("Hello World\0");
// char* to string?
string x = cast(string)x;
string x = cast(immutable(char)[])x;
Error!
Any ideas how to cast a char* to a string in D?
Use std.conv.to
to convert from char*
to string
. Use std.string.toStringZ
to go the other way.
import std.string;
import std.stdio;
import std.conv;
void main()
{
immutable(char)* x = "Hello World".toStringz();
auto s = to!string(x);
writeln(s);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With