www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Casting gremlins

reply Todd <toddtitus mindspring.com> writes:
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?

Todd
'newest newbee'
Oct 10 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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?
Oct 10 2007
parent reply Todd <toddtitus mindspring.com> writes:
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
Oct 10 2007
next sibling parent BCS <BCS pathlink.com> writes:
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).
Oct 10 2007
prev sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
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.
You're speaking of casting, so I assume you want to print out the value of each byte. And if you're trying to make it human-readable, you'll have to convert it. So, you want to change to ubyte. You have to use masks and shifts, or just shifts, for that, along with casts. int i = someFunc(); ubyte[4] bytes; bytes[0] = cast(ubyte)((i << 0) >> 24); bytes[1] = cast(ubyte)((i << 8) >> 16); bytes[2] = cast(ubyte)((i << 16) >> 8); bytes[3] = cast(ubyte)((i << 24) >> 0); Then to get a human-readable form of these, you can call std.string.toString on them. If, however, you merely one a human-readable form of the integer, just use std.string.toString on it.
 Thanks for the reply,
 Todd
 
Oct 10 2007
parent Todd Titus <toddtitus mindspring.com> writes:
Christopher Wright Wrote:

 If, however, you merely one a human-readable form of the integer, just 
 use std.string.toString on it.
I used toString() on it. with the same results. I have found 2 additional errors,(they multiply like rabbits). 1st. It compiles and links ok, and will output correctly non-stop, but if I recompile it without making any changes it won't output. compile it again and it works. seems like an inconsistent compile bug ?? 2nd. I had to substitute a known value, (to bypass-rule out any hardware issues) in the above compile. I found, or rather isolated it to an 'if' statement that seems to mask the output. I'll try to narrow it down further in the morning.
Oct 11 2007