Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Conversion Made Easy?! (http://www.go4expert.com/forums/conversion-made-easy-t2411/)

danielakkerman 1Jan2007 21:59

Conversion Made Easy?!
 
Hello,
Recently, After resolving major issues thanks to your patience and help, I've encountered another problem:
Code:

int x = 123;
Now, I am aware of the method to covert int to char(array):
Code:

int x = 123;
char y[25];
sprintf(y, "%d", x);

Here's the problem:
I need to concatenate x to y(place x at the END of y), which something that I was able to accomplish since 'sprintf' basically overwrites whatever was previously stored in y.
Any suggestions?
Thanks again,
Thanks,
Daniel

DaWei 1Jan2007 23:34

Re: Conversion Made Easy?!
 
Here's how this works:
I want to convert a number to a string.
I want to convert a number to a string and concatenate it to another string.
What do I need to know?
a) How to convert a number to a string.
b) How to concatenate two strings.
What do I know?
a) How to convert a number to a string.
What do I need to know?
a) How to concatenate two strings.

Once you know these two things, the job can be done. First pass, you'll probably make two strings, convert two numbers, and concatenate the second string to the first. Second pass, you'll look at your code and ask yourself how to simplify it. When you realize that a pointer to a string is simply a pointer to the first element of the string, and that a pointer can point at any element, thanks to pointer arithmetic, then you're home free.

Notice that I don't need int y if I'm willing to lose int x. I can simply replace int x with the new value at the appropriate point in the process. Further, I don't need either int if I can get away with putting the literal value directly in the sprintf statement. What you will need is knowledge of your circumstances, which will vary from case to case. This is part of the design phase, which always comes before you sit down at the keyboard.
Code:

#include <stdio.h>

int main (void)
{
        int x = 123;
        int y = 456;
        char theCombo [256];
        sprintf (theCombo,"%d", x);
        printf ("First step: %s\n", theCombo);
        sprintf (theCombo + strlen (theCombo), "%d", y);
        printf ("Second step: %s\n", theCombo);
        return 0;
}

Quote:

Originally Posted by Output
First step: 123
Second step: 123456


danielakkerman 2Jan2007 19:01

Re: Conversion Made Easy?!
 
Hi,
It's finally working, Thanks.
Daniel

shabbir 2Jan2007 19:06

Re: Conversion Made Easy?!
 
I can give a try on one more way but it should be a similar to DaWei but in less steps

sprintf(y, "%s%d",y,x);

It will append the x to y but I have not executed it but there can be an issue with \0 of the previous content of y and you can try and see what is the output.

DaWei 2Jan2007 22:06

Re: Conversion Made Easy?!
 
Good idea. "sprintf (y, "%s%d", y, x);" will definitely work the same as "sprintf (y + strlen (y), "%d",x);". Since sprintf is set up to concatenate, it'll handle the '\0' just fine.


All times are GMT +5.5. The time now is 07:00.