 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Slopey
Joined: 31 Jul 2005 Posts: 24
|
Posted: Wed Jul 26, 2006 9:07 am Post subject: Convert number to a string |
|
|
Hi All,
What's the easiest way to convert a number to a string (for example an integer?) in C/C++?
In the past Ive used itoa but I believe thats non-standard.
Thanks in advance - it's been a while since I've done any C/C++ and this one has complete escaped me - it's also missing from my books :/
Cheers,
S. |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Wed Jul 26, 2006 9:20 am Post subject: |
|
|
Either use itoa as you said, or do the dirty way with sprintf.
| Code: |
int i = 5023;
char number[32];
snprintf(number,32,"%i",i);
|
|
|
| Back to top |
|
 |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Wed Jul 26, 2006 9:59 am Post subject: |
|
|
| Raphael wrote: | Either use itoa as you said, or do the dirty way with sprintf.
| Code: |
int i = 5023;
char number[32];
snprintf(number,32,"%i",i);
|
|
Why is "sprintf" dirty...? Really, i'm new and I use this...?
| Code: | char buffer[200];
float num = 20.39999999;
sprintf(buffer,"Why is this wrong...? Does this float look nice %.02...?",num); |
If there is something wrong with that please tell us...?
ADe
P.S. Apart from declaring more bytes for the char than were needed... :D |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Wed Jul 26, 2006 11:08 am Post subject: |
|
|
| ADePSP wrote: |
Why is "sprintf" dirty...? Really, i'm new and I use this...?
|
Well, it's not really dirty, I just called it that in this case, as it needs the extra line to declare your buffer ;)
| ADePSP wrote: |
| Code: | char buffer[200];
float num = 20.39999999;
sprintf(buffer,"Why is this wrong...? Does this float look nice %.02...?",num); |
If there is something wrong with that please tell us...?
|
well, I think it should be %.02f but other than that it's perfectly fine |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|