converting a byte array to a struct array?
grauzone
none at example.net
Tue Dec 29 13:45:51 PST 2009
Trass3r wrote:
> I got some RGB palette in a byte array which I'd like to convert or
> "map" to an RGB struct array, isn't this easily possible without using
> dozens of struct constructors?
>
>
> RGB[256] PALETTE = cast(RGB[256]) [
> 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
> 0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...
>
> doesn't work cause of "non-constant expression"
>
> RGB[256] PALETTE = (cast(RGB[]) [
> 0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
> 0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...
> ]) (0 .. 256);
>
> compiles, but yields empty structs (and doesn't seem right anyway).
You've hit both an anti-feature and a bug.
First the anti-feature: [0xAB, ...] will yield an int[], not a ubyte[]
(I guess ubyte[] is what you're expecting). If you cast two arrays, the
compiler will reinterpret cast all data. The result won't be what you
intended.
Second, the bug: casting arrays at compiletime seems to behave
differently from casting at runtime. Casting at compiletime doesn't
reinterpret cast, it does conversion! Demonstration here:
http://codepad.org/OGjXADdu
Feel free to file some bug reports.
More information about the Digitalmars-d-learn
mailing list