Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 29, 2009 converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
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). |
December 29, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | Just remembered the old casting hack: RGB[] PALETTE = (cast(RGB*) cast(ubyte[]) [...])[0..256]; It works but is there perhaps another nicer possibility? |
December 29, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | On Tue, 29 Dec 2009 07:56:04 -0500, Trass3r <mrmocool@gmx.de> 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).
What is RGB's structure?
I would expect something like this to work:
RGB[256] PALETTE = [
{0x00, 0x00, 0x00},
{0xE3, 0x53, 0x00},
...
];
Assuming RGB is a struct of 3 ubyte members...
-Steve
|
December 29, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer schrieb:
> What is RGB's structure?
>
> I would expect something like this to work:
>
> RGB[256] PALETTE = [
> {0x00, 0x00, 0x00},
> {0xE3, 0x53, 0x00},
> ...
> ];
>
> Assuming RGB is a struct of 3 ubyte members...
>
Yeah, simply 3 ubytes.
But IIRC I read that struct literals will be removed in D2 so I didn't test that approach.
|
December 29, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r <mrmocool@gmx.de> wrote:
> Steven Schveighoffer schrieb:
>> What is RGB's structure?
>> I would expect something like this to work:
>> RGB[256] PALETTE = [
>> {0x00, 0x00, 0x00},
>> {0xE3, 0x53, 0x00},
>> ...
>> ];
>> Assuming RGB is a struct of 3 ubyte members...
>>
>
> Yeah, simply 3 ubytes.
> But IIRC I read that struct literals will be removed in D2 so I didn't test that approach.
You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose.
-Steve
|
December 29, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Tue, 29 Dec 2009 10:37:43 -0500, Steven Schveighoffer <schveiguy@yahoo.com> wrote: > On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r <mrmocool@gmx.de> wrote: > >> Steven Schveighoffer schrieb: >>> What is RGB's structure? >>> I would expect something like this to work: >>> RGB[256] PALETTE = [ >>> {0x00, 0x00, 0x00}, >>> {0xE3, 0x53, 0x00}, >>> ... >>> ]; >>> Assuming RGB is a struct of 3 ubyte members... >>> >> >> Yeah, simply 3 ubytes. >> But IIRC I read that struct literals will be removed in D2 so I didn't test that approach. > > You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose. And the answer (created by bearophile): http://codepad.org/8HnF62s2 -Steve |
December 29, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | 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. |
December 30, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone schrieb: > 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. > Yeah I already encountered that problem. That's why I had to cast(ubyte[]) it (where conversion is expected) for my reinterpret casting: RGB[] PALETTE = (cast(RGB*) cast(ubyte[]) [...])[0..256]; > 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 > You're right. But it only concerns casting to a struct(?) or an array of structs, right?!. Conversion is correct for standard arrays, but struct arrays need to be reinterpret cast (correct me if I'm wrong, I can't imagine how conversion could be reasonably used. Only thing might be casting an array of structs to another array of structs but I don't know any valid use-case). |
December 30, 2009 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer schrieb:
>> You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose.
>
> And the answer (created by bearophile): http://codepad.org/8HnF62s2
>
Yeah, I know but if you got that data out of some file like the resource section of a game exe in my case, adding the struct constructors is cumbersome.
|
January 03, 2010 Re: converting a byte array to a struct array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to grauzone | grauzone schrieb: > 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. Just found out this is stated as a feature in the docs. http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral http://codepad.org/bvk63OPw |
Copyright © 1999-2021 by the D Language Foundation