LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-30-2004, 09:21 PM   #1
totti10
Member
 
Registered: Oct 2004
Posts: 52

Rep: Reputation: 15
C how to convert an integer to string?


what a noob question, but i really do not know how to do it.
for string to integer, we can use atoi,but how about integer to string.
thankx in advance.
 
Old 10-30-2004, 09:37 PM   #2
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo (main), Gentoo (server)
Posts: 94

Rep: Reputation: 15
Looks like sprintf() is the function to investigate, although I am not that familiar with C/C++, so I may be off.
 
Old 10-30-2004, 09:56 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
yeah, it's a messy problem. The problem with sprintf or snprintf is that you need to figure out what is the longest string your int could turn into, so you can make the right size array. If you're on linux, you might be able to do
Code:
#define _GNU_SOURCE
#include <stdio.h>
...
char *s;
asprintf(&s, "%d", myint);
...
free(s);
Still not very pretty.
 
Old 10-30-2004, 10:11 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 53
I would just use snprintf():
Code:
int main(void)
{
  int num=86;
  char buf[50];

  snprintf(buf, 50, "%d", num);
  return 0;
}

Last edited by itsme86; 10-30-2004 at 10:12 PM.
 
Old 10-31-2004, 09:30 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 459

Rep: Reputation: 58
Possible pseudo code:
Quotient=InVal;
OutStr="";
While Quotient > 10 {
Reminder=(Quotient \ 10) /* Quotient mod 10 */
OutStr=Reminder+OutString; /* strcat */
Quotient = (Quotient - Reminder) / 10;
}
If Quotient > 0 {
OutStr=Quotient+OutString; /* strcat */
}

End
 
Old 10-31-2004, 12:38 PM   #6
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 15
Due to itoa() doesn't exist (i think), you must use sprintf() function as described above.
 
Old 10-31-2004, 01:43 PM   #7
totti10
Member
 
Registered: Oct 2004
Posts: 52

Original Poster
Rep: Reputation: 15
i successfully done it using snprintf,thxs lot guys.
 
Old 11-04-2004, 11:45 PM   #8
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
yes, itoa does exist.
 
Old 11-10-2005, 03:02 AM   #9
arpan
LQ Newbie
 
Registered: Nov 2005
Posts: 1

Rep: Reputation: 0
itoa() function is only provided in Turbo C but not in Linux
 
Old 11-10-2005, 04:46 AM   #10
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 717

Rep: Reputation: 33
sprintf is a C function.
But I am going to ask for C++.
How can I do it.
 
Old 11-10-2005, 05:49 AM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
for c++ just use a func like so
Code:
#include <sstream>
#include <iostream>
using std::ostringstream;
using std::flush;
using std::string;

string int2string(int num)
{
  ostringstream mystream; 
  mystream << num << flush; 
  return(mystream.str()); 
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ string to integer conversion fatman Programming 6 06-18-2006 08:17 AM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM
convert integer to string (atoi?) lea Programming 6 10-11-2002 05:16 PM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration