Regarding hex strings
Dejan Lekic
dejan.lekic at gmail.com
Mon Oct 22 04:18:01 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[]
>
>
> Generally I want to use hex strings to put binary data in a
> program, so usually it's a ubyte[] or uint[].
>
> So I have to use something like:
>
> auto data3 = cast(ubyte[])(x"A1 B2 C3 D4".dup);
>
>
> So maybe the following literals are more useful in D2:
>
> ubyte[] data4 = x[A1 B2 C3 D4];
> uint[] data5 = x[A1 B2 C3 D4];
> ulong[] data6 = x[A1 B2 C3 D4 A1 B2 C3 D4];
>
> Bye,
> bearophile
+1 on this one
I also like the x[ ... ] literal because it makes it obvious that
we are dealing with an array.
More information about the Digitalmars-d
mailing list