www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Simple, but how?

reply schnu <schnu_member pathlink.com> writes:
Hello,

I'm new to D programming and tried to convert an int to a char[]. Using
str.string.toString() led to an access violation. I also didn't find those short
c style conversion functions (like atoi etc.). Is there a way to do this
conversion?
The code that results in an error is:

char[] s=std.string.toString((cast(TreeNode)c).text.length);
printf(info~s);
Feb 15 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
It looks to me like your error (Access Violation) is probably in the 
expression 'cast(TreeNode)c' and has nothing to do with 
std.string.toString() at all.  Is 'c' a valid instance, assuming 
TreeNode is a class?  Did you test with a dummy statement, something 
like 'char[] dummy = std.string.toString(1234);' ?

And if you want 'atoi()' et al, just import std.conv and there you are.

-- Chris S

schnu wrote:
 Hello,
 
 I'm new to D programming and tried to convert an int to a char[]. Using
 str.string.toString() led to an access violation. I also didn't find those
short
 c style conversion functions (like atoi etc.). Is there a way to do this
 conversion?
 The code that results in an error is:
 
 char[] s=std.string.toString((cast(TreeNode)c).text.length);
 printf(info~s);
 
 
 
Feb 15 2005
next sibling parent reply schnu <schnu_member pathlink.com> writes:
Thank you, Chris. Your assumption was correct. I tried to use 'c' outside my
main loop (where c was checked). And also thanks for your hints. They will
probably help me a lot.

Regards,
schnu

In article <cuteqo$13fn$1 digitaldaemon.com>, Chris Sauls says...
It looks to me like your error (Access Violation) is probably in the 
expression 'cast(TreeNode)c' and has nothing to do with 
std.string.toString() at all.  Is 'c' a valid instance, assuming 
TreeNode is a class?  Did you test with a dummy statement, something 
like 'char[] dummy = std.string.toString(1234);' ?

And if you want 'atoi()' et al, just import std.conv and there you are.

-- Chris S

schnu wrote:
 Hello,
 
 I'm new to D programming and tried to convert an int to a char[]. Using
 str.string.toString() led to an access violation. I also didn't find those
short
 c style conversion functions (like atoi etc.). Is there a way to do this
 conversion?
 The code that results in an error is:
 
 char[] s=std.string.toString((cast(TreeNode)c).text.length);
 printf(info~s);
 
 
 
Feb 15 2005
parent Chris Sauls <ibisbasenji gmail.com> writes:
Quite welcome.  Its a fairly common beginner's mistake. :) Hell, I still 
do it now and then, sadly/admittedly.

-- Chris S

schnu wrote:
 Thank you, Chris. Your assumption was correct. I tried to use 'c' outside my
 main loop (where c was checked). And also thanks for your hints. They will
 probably help me a lot.
 
 Regards,
 schnu
 
Feb 15 2005
prev sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 15 Feb 2005 12:33:57 -0600, Chris Sauls <ibisbasenji gmail.com>  
wrote:
 It looks to me like your error (Access Violation) is probably in the  
 expression 'cast(TreeNode)c' and has nothing to do with  
 std.string.toString() at all.
Or, it could be the printf printf(info~s); isn't a null terminated C string, it's a char * to a D string which is not null terminated. Regan
 Is 'c' a valid instance, assuming TreeNode is a class?  Did you test  
 with a dummy statement, something like 'char[] dummy =  
 std.string.toString(1234);' ?

 And if you want 'atoi()' et al, just import std.conv and there you are.

 -- Chris S

 schnu wrote:
 Hello,
  I'm new to D programming and tried to convert an int to a char[]. Using
 str.string.toString() led to an access violation. I also didn't find  
 those short
 c style conversion functions (like atoi etc.). Is there a way to do this
 conversion?
 The code that results in an error is:
  char[] s=std.string.toString((cast(TreeNode)c).text.length);
 printf(info~s);
Feb 15 2005
parent Chris Sauls <ibisbasenji gmail.com> writes:
True enough, I missed that from the original post.  Another good reason 
to use std.stdio.writef() instead of the (hopefully not for long) 
auto-imported printf. :)

-- Chris S

Regan Heath wrote:
 Or, it could be the printf
Feb 15 2005
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"schnu" <schnu_member pathlink.com> wrote in message
news:cuteag$134l$1 digitaldaemon.com...
 Hello,

 I'm new to D programming and tried to convert an int to a char[]. Using
 str.string.toString() led to an access violation. I also didn't find those
short
 c style conversion functions (like atoi etc.). Is there a way to do this
 conversion?
std.string.format(i) will convert i to a string.
Feb 16 2005
parent Chris Sauls <Chris_member pathlink.com> writes:
I keep forgetting about format()...

So on a side note, when will printf() get "cycled out" of D's default
environment?  I mean it was nice and all in the beginning, but now that we have
std.stdio to use, is it really neccessary?

-- Chris Sauls

In article <cv0d3v$13dn$1 digitaldaemon.com>, Walter says...
std.string.format(i)

will convert i to a string.
Feb 16 2005