convert string to ubyte[]

bauss jj_1337 at live.dk
Sun Nov 12 00:13:28 UTC 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.


More information about the Digitalmars-d-learn mailing list