Regarding hex strings

bearophile bearophileHUGS at lycos.com
Wed Oct 17 16:58:25 PDT 2012


Maybe hex strings were invented in D1 when strings were 
convertible to char[]. But today strings are an array of 
immutable UFT-8, so I think this default type is now less useful:

void main() {
     string data1 = x"A1 B2 C3 D4"; // OK
     immutable(ubyte)[] data2 = x"A1 B2 C3 D4"; // error
}


Gives:
test.d(3): Error: cannot implicitly convert expression 
("\xa1\xb2\xc3\xd4") of type string to ubyte[]


Generally I'd like to use hex literals to put binary data nicely 
in a program, so usually I need an 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[]  data4a = x[A1 B2 C3 D4];
ubyte[4] data4b = x[A1 B2 C3 D4];
uint[]   data5a = x[A1 B2 C3 D4];
uint[2]  data5b = x[A1 B2 C3 D4];
ulong[]  data6a = x[A1 B2 C3 D4 A1 B2 C3 D4];
ulong[1] data6b = x[A1 B2 C3 D4 A1 B2 C3 D4];

Bye,
bearophile


More information about the Digitalmars-d-bugs mailing list