convert string to ubyte[]
Guillaume Piolat
contact at spam.com
Sat Nov 11 15:47:50 UTC 2017
On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote:
> Hello,
>
> This will be trivial question but I cannot figure out
> what's wrong. I want to convert string to an array of ubyte.
>
> import std.conv;
> void main() {
> auto s = "hello";
> ubyte[] b = to!(ubyte[])(s);
> }
>
> It compiles but cause run time error:
>
> std.conv.ConvException at C:\APP\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3530): Can't parse string: "[" is missing
>
> I cannot understand the meaning of this message.
> Replacing s with s.dup to remove immutable doesn't help.
> Do I need to use cast?
>
> Regards,
> Aki
to!(ubyte[]) is a semantic transformation that tries to parse an
array literal it seems.
You can use slice casting instead:
import std.conv;
void main() {
auto s = "hello";
ubyte[] b = cast(ubyte[])(s.dup);
}
More information about the Digitalmars-d-learn
mailing list