Thread overview
convert string to ubyte[]
Nov 11, 2017
aki
Nov 11, 2017
Guillaume Piolat
Nov 11, 2017
Mike Parker
Nov 11, 2017
user1234
Nov 12, 2017
bauss
Nov 12, 2017
aki
November 11, 2017
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@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

November 11, 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@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);
     }

November 11, 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@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

I don't know about the error you're seeing, but the generic way to get an array of the underlying data type of a string is via std.string.representation.

import std.string;
auto s = "hello";
auto bytes = s.representation;

https://dlang.org/phobos/std_string.html#.representation

You can also simply cast to the appropriate type if you already know what type of string you have.

auto bytes = cast(immutable(ubyte)[])s;

Of course, if you need a mutable array you should dup:

auto bytes = cast(ubyte[])s.dup;
November 11, 2017
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote:
> On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote:
> auto bytes = cast(immutable(ubyte)[])s;
>
> Of course, if you need a mutable array you should dup:
>
> auto bytes = cast(ubyte[])s.dup;

Not only "should" but he "must" otherwise with string literals he'll get a violation access error.
November 12, 2017
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote:
> On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote:
>> [...]
>
> I don't know about the error you're seeing, but the generic way to get an array of the underlying data type of a string is via std.string.representation.
>
> import std.string;
> auto s = "hello";
> auto bytes = s.representation;
>
> https://dlang.org/phobos/std_string.html#.representation
>
> You can also simply cast to the appropriate type if you already know what type of string you have.
>
> auto bytes = cast(immutable(ubyte)[])s;
>
> Of course, if you need a mutable array you should dup:
>
> auto bytes = cast(ubyte[])s.dup;

That function needs to be highlighted more through documentation. I've always implemented my own versions to achieve the same as representation. I had no idea that function existed.

If just I knew.
November 12, 2017
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote:
> auto s = "hello";
> auto bytes = s.representation;
>
> https://dlang.org/phobos/std_string.html#.representation

Thank you for the replay.
Now I know.

aki