Casting gremlins

BCS BCS at pathlink.com
Wed Oct 10 17:07:36 PDT 2007


Todd wrote:
> Frits van Bommel Wrote:
> 
> 
>>Todd wrote:
>>
>>>In C it was possible to cast a Uint to a Char[], however, I can't seem to get it to compile in D. 
>>>
>>>example: I have 
>>>
>>>uint data1;
>>>long data;
>>>long lblonedata;
>>>.
>>>.
>>>.
>>>char[] dataone = cast(char[]) data1;
>>>lblonedata = atoi(dataone);
>>>
>>>I'm importing std.string, std.conv;
>>>
>>>and get e2ir: cannot cast uint to char[]
>>>
>>>I was using dmd 1.022 and switched to 2.x today with no help.
>>>Does D use a different method? or I'm I missing something obvious?
>>
>>In C, an array is just a pointer. In D it also contains a length (unless 
>>it's a static array, in which case that's encoded in the type).
>>You could try casting to a pointer instead, but casting between pointer 
>>and non-pointer types usually means you're doing something wrong. What 
>>is it exactly that you're trying to achieve here?
> 
> 
> I have a function that returns an uint, we'll call it 'data1'. from there, I'm trying to cast it to a char to insert into a textbox, similiar to 'mini-calc'. I know it's simple, but it seems to be escaping me at the moment.
> 
> Thanks for the reply,
> Todd
> 


do you expect this?:

uint i = 57;

char[] c = cast(char[])i;

writef("%s\n", c); // prints "57"

AFAIK C won't do this. D has a function to do it. look in std.string for 
toString(uint).


More information about the Digitalmars-d-learn mailing list