Regarding hex strings

monarch_dodra monarchdodra at gmail.com
Thu Oct 18 02:47:27 PDT 2012


On Thursday, 18 October 2012 at 00:45:12 UTC, bearophile wrote:
> (Repost)
>
> hex strings are useful, but I think they were invented in D1 
> when strings were convertible to char[]. But today they are an 
> array of immutable UFT-8, so I think this default type is not 
> so useful:
>
> void main() {
>     string data1 = x"A1 B2 C3 D4"; // OK
>     immutable(ubyte)[] data2 = x"A1 B2 C3 D4"; // error
> }
>
>
> test.d(3): Error: cannot implicitly convert expression 
> ("\xa1\xb2\xc3\xd4") of type string to ubyte[]
>
> [SNIP]
>
> Bye,
> bearophile

The conversion can't be done *implicitly*, but you can still get 
your code to compile:

//----
void main() {
     immutable(ubyte)[] data2 =
         cast(immutable(ubyte)[]) x"A1 B2 C3 D4"; // OK!
}
//----

It's a bit ugly, and I agree it should work natively, but it is a 
workaround.


More information about the Digitalmars-d mailing list