Posts: 354
Threads: 11
Joined: May 2002
Here's one way of converting an int to string in C++. It seems very clunky, is there a better way?
Code:
#include <sstream>
ostringstream oss;
oss << 5;
string myString = oss.str();
char *cString = myString.c_str();
In a world without walls and doors, who needs Windows and Gates?
Posts: 1,166
Threads: 62
Joined: Apr 2003
Quote:#include <stdlib.h>
char *itoa(int value, char *string, int radix);
Description
Converts an integer to a string.
itoa converts value to a null-terminated string and stores the result in string. With itoa, value is an integer.
radix specifies the base to be used in converting value; it must be between 2 and 36, inclusive. If value is negative and radix is 10, the first character of string is the minus sign (-).
Note: The space allocated for string must be large enough to hold the returned string, including the terminating null character (\0). itoa can return up to 17 bytes.
Return Value
itoa returns a pointer to string.
Mmm, gooooogle....
am an asshole. Get used to it.
Posts: 3,368
Threads: 195
Joined: Jan 2003
I don't see the reason why a purportedly (SP?) efficient language like C would even have a string conversion. Strings are made up of bytes, so why not just recognize them as dynamic byte (or integer or double integer) arrays, and when using them as 'a$ = "Hi I'm a string"' just have the compiler convert to the array equivalent?
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 691
Threads: 5
Joined: Apr 2002
Strings are arrays of chars:
Code:
#include <stdio.h>
#include <string.h>
int main() {
int i;
char *string = "Hello World\n";
for(i = 0; i < strlen(string); i++) {
printf("%c\n", string[i]);
}
}
String conversion exists for portability reasons. If you just grabbed the first byte of the above string you would have the value of the letter 'H' in whatever character set you are using (ascii, unicode, etc), the value isnt guaranteed to be the same everywhere, so the standard C library provides functions for doing the conversion. That way, only the library function every needs changing and any code that uses it will be portable to other character sets.
esus saves.... Passes to Moses, shoots, he scores!
Posts: 354
Threads: 11
Joined: May 2002
Thanks for the help.
What I really want to do is just load, say 10 bitmaps ("p1.bmp" to "p10.bmp") so where it's nice n easy in QB, I can see I'm just gonna have to mess around a bit more.
Code:
'assuming we had a bmpLoad sub ;)
for i = 1 to 10
bmpLoad "p"+ltrim$(str$(i))+".bmp"
next
sniff..
In a world without walls and doors, who needs Windows and Gates?
Posts: 2,020
Threads: 24
Joined: Jun 2002
well, the easy way is to use letters and ascii characters, off the top of my head
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 6,419
Threads: 74
Joined: Mar 2002
I used this trick to load from 0 to 9:
Code:
char filename = "tile_.txt";
for(i=0; i<10; i++)
{
filename[4] = i+ '0'; // Stupid conversion dec -> ASCII
load_stuff(filename);
}
Posts: 530
Threads: 17
Joined: May 2002
sprintf is also great, you can do something like
sprintf(string,"tile%d.bmp,looper);
and itll work fine
Posts: 6,419
Threads: 74
Joined: Mar 2002
yup, I forgot about that... *that's* the sollution
great, HR
Posts: 108
Threads: 40
Joined: Aug 2001
Code:
// int to string converter
string str ( int i )
{
char buffer[100];
_itoa(i, buffer, 10);
return (string(buffer));
}
COUNT HACKED BY RAZVEEE
RAZVEE IS A SCRIPT KIDDIE- hacker9