Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast a char* to string in D?

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?

like image 839
Gary Willoughby Avatar asked Jan 10 '12 23:01

Gary Willoughby


1 Answers

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);
}
like image 121
eco Avatar answered Sep 20 '22 07:09

eco