digitalmars.D - casting question
- Regan Heath (24/24) Jun 18 2004 Is this valid:
- Vathix (5/16) Jun 18 2004 They're just not compatible types. You can't cast a struct to an int eit...
- Regan Heath (21/42) Jun 19 2004 Do you mean "an int to a struct"? Why not? All you're saying is pretend
- =?ISO-8859-15?Q?Julio_C=E9sar_Carrascal_Urquijo?= (7/16) Jun 19 2004 Arrays in D are just a struct with a length field and a pointer to the
- Regan Heath (7/20) Jun 20 2004 Yes. And calculate the length. Is there an official way to do this? Or a...
Is this valid: ulong length = 1; ubyte[8] test; test[] = cast(ubyte[])length; or test = cast(ubyte[])length; or test[] = cast(ubyte[])length[0..8]; or ? if not, why not? what would it do exactly... - convert length from a ulong to a ubyte and assign to test[0], test[1], test[2], ..etc.. - point ubyte's data pointer at length (or a copy of length). I'm hoping for the latter because as ulong is 64 bit and ubyte is 8 bit and ubyte[8] is 64 bits worth of data it could do it. Without this possibility you're left going: memcpy(&test[0],length,length.sizeof); which, while not bad, is not true D style. Thoughts? Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 18 2004
"Regan Heath" <regan netwin.co.nz> wrote in message news:opr9tddfna5a2sq9 digitalmars.com...Is this valid: ulong length = 1; ubyte[8] test; test[] = cast(ubyte[])length; or test = cast(ubyte[])length; or test[] = cast(ubyte[])length[0..8]; or ? if not, why not?They're just not compatible types. You can't cast a struct to an int either. You can get around it using a pointer: test[] = (cast(ubyte*)&length)[0 .. length.sizeof];
Jun 18 2004
On Fri, 18 Jun 2004 21:49:52 -0400, Vathix <vathixSpamFix dprogramming.com> wrote:"Regan Heath" <regan netwin.co.nz> wrote in message news:opr9tddfna5a2sq9 digitalmars.com...Do you mean "an int to a struct"? Why not? All you're saying is pretend this is one of these, all it has to do is point at the address given and interpret it as the new type. So I don't see why this: test[] = cast(ubyte[])length; should not be allowed, it's explicit, and it makes sense that it should do exactly this: test[] = (cast(ubyte*)&length)[0 .. length.sizeof]; Given that you know the size of each type you could cast any array of any basic type to any other array of another basic type, or even a user defined type. Casting from a small type to a large type creates the case where there isn't enough data to make up a complete last item, in this case you can either pad or throw an "Error: lengths don't match for array copy" error. I think this is very useful.Is this valid: ulong length = 1; ubyte[8] test; test[] = cast(ubyte[])length; or test = cast(ubyte[])length; or test[] = cast(ubyte[])length[0..8]; or ? if not, why not?They're just not compatible types. You can't cast a struct to an int either.You can get around it using a pointer: test[] = (cast(ubyte*)&length)[0 .. length.sizeof];Thanks. This will help for now. But pointers really aren't the D thing. Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 19 2004
Regan Heath wrote:On Fri, 18 Jun 2004 21:49:52 -0400, Vathix <vathixSpamFix dprogramming.com> wrote:Arrays in D are just a struct with a length field and a pointer to the data. So what you need to do is change the pointer so that it points to the address where the ulong is stored. -- Julio César Carrascal Urquijo http://jcesar.f2o.org/They're just not compatible types. You can't cast a struct to an int either.Do you mean "an int to a struct"? Why not? All you're saying is pretend this is one of these, all it has to do is point at the address given and interpret it as the new type.
Jun 19 2004
On Sat, 19 Jun 2004 12:31:57 -0500, Julio César Carrascal Urquijo <adnoctum phreaker.net> wrote:Regan Heath wrote:Yes. And calculate the length. Is there an official way to do this? Or are you suggesting an ugly hack? Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/On Fri, 18 Jun 2004 21:49:52 -0400, Vathix <vathixSpamFix dprogramming.com> wrote:>>Arrays in D are just a struct with a length field and a pointer to the data. So what you need to do is change the pointer so that it points to the address where the ulong is stored.They're just not compatible types. You can't cast a struct to an int either.Do you mean "an int to a struct"? Why not? All you're saying is pretend this is one of these, all it has to do is point at the address given and interpret it as the new type.
Jun 20 2004