convert int to string

Closed Thread

Join Date: Sep 2004
Posts: 1
Reputation: cortiknee is an unknown quantity at this point 
Solved Threads: 0
cortiknee cortiknee is offline Offline
Newbie Poster

convert int to string

 
1
  #1
Sep 18th, 2004
I need to figure out how to append an int to a string, so basically i need to convert the int to a string 1st, but I cant figure out how. :rolleyes: any help would be greatly appreciated. thank you, courtney
Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: convert int to string

 
1
  #2
Sep 18th, 2004
Greetings,

This process is not hard since the default libraries included a function to do this. It's called itoa().

» char *itoa(int value, char *buffer, int radix);
Converts an integer value to a null-terminated string using the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by the minus sign (-). With any other radix, value is always considered unsigned.
buffer should be large enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.
Further information can be found here.

Here is an example of how to use this function:
#include <stdlib.h>	// for itoa() call
#include <stdio.h>	// for printf() call

int main() {
	int num = 123;
	char buf[5];

	// convert 123 to string [buf]
	itoa(num, buf, 10);

	// print our string
	printf("%s\n", buf);

	return 0;
}


I hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,349
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: convert int to string

 
0
  #3
Sep 18th, 2004
Originally Posted by Stack Overflow
This process is not hard since the default libraries included a function to do this. It's called itoa().
And itoa is nonstandard. A standard function is sprintf.
#include <stdio.h>
   
   int main(void)
   {
      const char base[] = "filename";
      char filename [ FILENAME_MAX ];
      int number = 42;
      sprintf(filename, "%s%d", base, number);
      printf("filename = \"%s\"\n", filename);
      return 0;
   }
   
   /* my output
  filename = "filename42"
  */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: convert int to string

 
0
  #4
Sep 18th, 2004
Originally Posted by Dave Sinkula
sprintf(filename, "%s%d", base, number);
Ah, yes. Though remember sprintf() does not calculate for buffer overflow. sprintf() doesn't flush any file buffer, in fact it doesn't have any buffer associated with it. sprintf() operates on a character buffer (not quite the same thing). If you overflow that buffer you can crash the system.

A safer version of sprintf() is snprintf() which unfortunately isn't very standard yet. It appears in various forms from many compiler vendors and was finally standardized in C99 and will hopefully make it as part of standard C++ in the next standard of C++. Until then you just have to dig up the unstandard version of it if you have one.

If you have no unstandard version of snprintf() you're sort of out of luck. You can of course always write your own version of sprintf() but it can be quite tricky unless you have your compiler's version of sprintf().

Here is an open-source version of snprintf() and others if interested:
snprintf.c - a portable implementation snprintf (etc...)

Writing your own version of itoa() on the contrary is quite simple. Just for reference, here is an example of how itoa() works:

#include <stdio.h>
//#include <string.h>

// Function declarations
// typedef __w64 unsigned int size_t
size_t strlen(const char *);
char *strrev(char *);
char *itoa(int, char *, int);

int main() {
	int num = 123;
	char buf[5];

	itoa(num, buf, 10);

	printf("%s\n", buf);

	return 0;
}

size_t strlen(const char *string) {
	const char *s;

	s = string;
	while (*s)
		s++;
	return s - string;
}

char *strrev(char *str) {
	char *p1, *p2;

	if (!str || !*str)
		return str;

	for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
		*p1 ^= *p2;
		*p2 ^= *p1;
		*p1 ^= *p2;
	}

	return str;
}

char *itoa(int n, char *s, int b) {
	static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
	int i=0, sign;
    
	if ((sign = n) < 0)
		n = -n;

	do {
		s[i++] = digits[n % b];
	} while ((n /= b) > 0);

	if (sign < 0)
		s[i++] = '-';
	s[i] = '\0';

	return strrev(s);
}

It isn't perfect, but it gets the job done.


- Stack Overfow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert int to string

 
0
  #5
Sep 18th, 2004
>Though remember sprintf() does not calculate for buffer overflow.
Neither does itoa.

>A safer version of sprintf() is snprintf() which unfortunately isn't very standard yet.
It is standard, but the standard just isn't widely implemented yet. As it is, C89 is the dominant C standard, and snprintf is nothing more than an extension on many compilers.

>Writing your own version of itoa() on the contrary is quite simple.
Yes, but doing it correctly is not as trivial as some would have you believe.

>*p1 ^= *p2;
>*p2 ^= *p1;
>*p1 ^= *p2;
It's beyond me why people still use this.
Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: convert int to string

 
0
  #6
Sep 18th, 2004
» Neither does itoa.
Exactly, though now there is a working prototype. It wouldn't be hard for someone to modify it and make it safer. It is alot better off than re-writing sprintf().

» It is standard, but the standard just isn't widely implemented yet.
Standard only in C99.

» It's beyond me why people still use this.
Bitwise XOR Operator ^
a^b: 1 if both bits are different. 3 ^ 5 is 6.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,349
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: convert int to string

 
0
  #7
Sep 18th, 2004
Originally Posted by Stack Overflow
» It's beyond me why people still use this.
Bitwise XOR Operator ^
a^b: 1 if both bits are different. 3 ^ 5 is 6.
Yes, I think several of us know this swap trick. To others it may be confusing. It might even be confusing to the compiler such that it doesn't optimize as well as the straightforward approach. Other hacks too.

>>I need to figure out how to append an int to a string
>Ah, yes. Though remember sprintf() does not calculate for buffer overflow.

You must be on a platform with some really huge ints :lol: (whose decimal representation size, by the way, can be accounted for at compile time).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert int to string

 
0
  #8
Sep 19th, 2004
>It wouldn't be hard for someone to modify it and make it safer.
Don't forget that someone would also have to modify it to make it correct as well as safer. I was implying previously that your code has a bug.

» It is standard, but the standard just isn't widely implemented yet.
>Standard only in C99.
Yes, that would be the standard that isn't widely implemented yet. Unless you know of any others that is.

>Bitwise XOR Operator ^
>a^b: 1 if both bits are different. 3 ^ 5 is 6.
I'm well aware of what it does and how it does it. What confuses me is why people still think that it's a neat trick. The XOR swap is difficult to get right because of obscurity, and when it is right, it isn't very flexible. On top of that, it may not be as efficient as you think.
Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: spincycle is an unknown quantity at this point 
Solved Threads: 0
spincycle spincycle is offline Offline
Newbie Poster

Re: convert int to string

 
0
  #9
Mar 7th, 2007
Originally Posted by cortiknee View Post
I need to figure out how to append an int to a string, so basically i need to convert the int to a string 1st, but I cant figure out how. :rolleyes: any help would be greatly appreciated. thank you, courtney
hope this helps :

  1.  
  2. int i = 42; // number to convert
  3. char buffer[3]; // 3 digit buffer
  4. char* output; //output string (already filled with something)
  5.  
  6. sprintf(buffer, "%d", i);
  7. strcat(output, buffer);
Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: convert int to string

 
0
  #10
Mar 7th, 2007
Don't new people bother looking at dates? This thread is only 3 years old... :eek:
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC