This is so simple I’m embarrassed to ask, but how do you convert a c string to a d string in D2?
I’ve got two use cases.
string convert( const(char)* c_str );
string convert( const(char)* c_str, size_t length );
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use std.string.toString(char*) (D1/Phobos) or std.conv.to!(string) (D2):
Slice the pointer:
(you can’t use “length” because it has a special meaning with the slice syntax).
Both will return a slice over the C string (thus, a reference and not a copy). Use the .dup property to create a copy.
Note that D strings are considered to be in UTF-8 encoding. If your string is in another encoding, you’ll need to convert it (e.g. using the functions from std.windows.charset).