Click to See Complete Forum and Search --> : Convert Int


cheahyw
03-07-2004, 10:28 PM
Hi,

i had generated a sequence of num using FOR loop with following
output result on screen

9001\
9002\
9003\....

and i want to convert this output to string. is there way other that using strcpy because keep having problem using the statement "strcpy(string, array[j])" where array[j] is the increment count from the FOR loop.

thanks.

Danny
03-08-2004, 12:22 AM
you can't use strcpy to convert an int to a C-string. You need to use atoi() or sprintf() for this operation. Better yet, use the <sstream> library: http://gethelp.devx.com/techtips/cpp_pro/10min/2001/april/10min0401.asp

cheahyw
03-08-2004, 10:31 PM
oh. thanks alot managed to convert it into string. i am trying to load all these string into array & there is no err during the compiling but err prompted when running the program. any idea?

for(i=9000;i<10000;i++)
{
array[j] = i;
temp = array[j];
printf("Packet[%d] = %d\n", j, temp);
sprintf(msg1, "%d\\\n", temp);
printf("%s", msg);
msg1[j]= msg[j][7];
printf("%s", msg1[j]);
j++;
}

theoretically, i think this is correct because i want to load each of these string (9001\, 9002\...) into msg1[0]=9001\, msg[1]=9002\...

jonnin
03-08-2004, 11:52 PM
I think this does the same exact thing unless you needed
the array variable loaded too. look at the comment for your crash? something is wrong with msg, either its a string or an
array of string... which?!



for(i=9000;i<10000;i++)
{
printf("Packet[%d] = %d\n", j, i);
sprintf(msg1, "%d\\\n", i);
printf("%s", msg); //should this be message 1? msg is a pointer as its a defined below? something is wrong here.
printf("%s", msg[j][7]);
j++;
}

Danny
03-09-2004, 10:48 PM
You can't use a char [][] i.e. an array of an array as a C-string. Again, I suggest looking at a std::string based solution instead of the <cstdio> mess:
http://www.devx.com/getHelpOn/10MinuteSolution/16975
Now the link should work.

cheahyw
03-10-2004, 05:54 AM
but my requirement is to load those string that generated using FOR loop & sprintf into an array like following but not to check the validation of data
msg[0]=9001\
msg[1]=9002\
msg[2]=9003\....

below is the code that i made some amendment on it and it can ran but giving me the garbage

char msg[10][10],msg1[10];
for(i=9000;i<9050;i++)
{
printf("Packet[%d] = %d\n", j, i);
sprintf(msg1, "%d\\\n", temp);
printf("%s", msg1);
msg[j][10]= msg1[10];
printf("%s", msg[j]); //HERE PRINTED OUT THE GARBAGE
j++;
}

jonnin
03-10-2004, 08:28 AM
char msg[10][10],msg1[10];
for(i=9000;i<9050;i++)
{
printf("Packet[%d] = %d\n", j, i);
sprintf(msg1, "%d\\\n", temp);
^^^^^^^^ <------- what is temp?!
should it be i?


printf("%s", msg1);

msg[j][10]= msg1[10]; //what is this for?
you cant assign arrays like this, if thats what you think it does.
this assigns one char from msg1 in 10th spot to one location in msg.

use memcpy or strcpy to do this.


printf("%s", msg[j]); //HERE PRINTED OUT THE GARBAGE
j++;
}

cheahyw
03-10-2004, 11:54 PM
if im using memcpy, i think it just copy the whole string into another location, thats all. i added another FOR loop in my code to load those data into array as following but there is unwanted char just after my data.

when trying to check those data that loaded into array, it shown the whole things as following.

//MY CODE
char msg[10][5],msg1[5];
int i,k, j=0;
/* create packet & load packet into RAM using array */
for(i=9000;i<9010;i++)
{
printf("\nPacket[%d] Generated = %d\n", j, i);
sprintf(msg1, "%d\\", i);
for(k=0;k<5;k++)
msg[j][k]= msg1[k];
printf("After K FOR LOOP : PACKET[%d] = %s", j, msg[j]);
j++;
}
printf("\nAfter i FOR LOOP : PACKET[%d] = %s\n", 1, msg[1]);

//OUTPUT ON SCREEN

Packet[0] Generated = 9000
After K FOR LOOP : PACKET[0] = 9000\╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕
Packet[1] Generated = 9001
After K FOR LOOP : PACKET[1] = 9001\╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕
.
.
.
.
After K FOR LOOP : PACKET[9] = 9009\╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕
After i FOR LOOP : PACKET[1] = 9001\9002\9003\9004\9005\9006\9007\9008\9009\╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕

jonnin
03-11-2004, 12:17 AM
initialize the strings to all zero (not the printed zero, the integer zero). the final char in all strings must be zero. That should finish it up for you.

memset is good for this.
that may be the difference in strcpy and memcpy, str may take care of it for you. I havent used string.h in a long time.

cheahyw
03-11-2004, 01:46 AM
its work after initialize msg1 & msg to 0 but not sure what you mean by "the final char in all strings must be zero".

for the final count, it still have the unwanted char. How to clear all these char "╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕" ??

After K FOR LOOP : PACKET[9] = 9009\╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕

another thing is when i trying to printf the msg[1], it shown the entire data as below

After i FOR LOOP : PACKET[1] = 9001\9002\9003\9004\9005\9006\9007\9008\9009\╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└_↕

but what i want after the printf should be like these, is they a way to "break" it?
After i FOR LOOP : PACKET[1] = 9001\
After i FOR LOOP : PACKET[2] = 9002\
.
.
.

Danny
03-11-2004, 02:27 AM
C-string are implemented as an array of char with the last element containing the '\0' character. This way, C functions recognize the end of a string. failing to include a null charcater at the end of an array causes all these function to bhave in an unpredictable manner. This is a fundamental issue of C programming. If you're not familiar with it, you should first master this topic and then move on to more advanced topics such as int to string conversion. There's no point in working so hard when you don't know the basics yet.

jonnin
03-11-2004, 06:55 AM
Its fairly predictible on ever compiler i've ever seen: the print statements expect a zero, and by golly they are gonna travel through ram, printing away, until you either crash on an access violation or encounter a zero at random.