I have a standard char pointer that I am trying to pass 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 on how to distinguish char * from string in D?
Use std.conv.to to convert from char* to string . Use std.string.toStringZ to go the other way.
std.conv.to
char*
string
std.string.toStringZ
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 know the exact length, you can do this:
immutable(char)* cptr = obj.SomeSource(); int len = obj.SomeLength(); string str = cptr[0..len];
In some cases (for example, if the string contains \0 ), which is necessary.
\0
Source: https://habr.com/ru/post/905667/More articles:Kcachegrind / callgrind does not match dispatcher functions? - valgrindHow to zlib compress QByteArray? - c ++How are AJAX proxies with Rack middleware? - ajaxInconsistent results with java threads - javaHow to hide text box cursor using CSS? - cssXDocument.Save () removes my & # xA; legal entities - c #Get xsi: type with xpath - xmlAdvanced download of Asp.Net files - c #Using SqlFunctions or equivalent EdmFunctions collection with MySql and Entity Framework - mysqlConvert IFile to file - javaAll Articles