LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 10-10-2002, 07:12 AM   #1
lea
Member
 
Registered: Jul 2002
Posts: 31

Rep: Reputation: 15
convert integer to string (atoi?)


Hi all!

I would like to convert an integer-variable into a string.

* Where do I get atoi (*.h)? resp. how do I apply this function?

* Is there a smarter way (without atoi) to program this:

int age=12;
char string[]="My Age: ";
strcpy(string, itoa(age));

TIA
Lea
 
Old 10-10-2002, 12:14 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
First thing: atoi converts string to an integer.
You need to use another function. I can't recall an name now, but it's quite easy. When you've got a number, you make a copy of it, divide it by 10 and see what's left (modulo 10). Then you write the right character to a table and to the same with the orginal number divided by 10. You do this until the number is 0.
 
Old 10-10-2002, 01:10 PM   #3
Ztyx
Member
 
Registered: Dec 2001
Location: Stockholm, Sweden
Distribution: Ubuntu, Kubuntu and Debian
Posts: 338

Rep: Reputation: 30
This is much simpler:

Code:
#include <stdio.h>

int age=12;
char string[200];
sprintf(string, "My age: %d", age);
The sprintf works as printf although it writes its output to a string instead of stdout (the screen).
For information about the sprintf() function I refer to "man sprintf".
 
Old 10-10-2002, 01:35 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Right, it's simplier.
 
Old 10-11-2002, 12:51 AM   #5
lea
Member
 
Registered: Jul 2002
Posts: 31

Original Poster
Rep: Reputation: 15
I like this forum.
Thanks for your replies and the help.
LEA
 
Old 10-11-2002, 03:10 PM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Ztyx is very correct, however, I would highly suggest that you use the snprintf function. This allows you to set a cap length of the string, so that no buffer overruns happen and you get nasty
segfaults or some other such a thing.
Code:
#include <stdio.h>
&nbsp;
int main(void)
{ 
  int retVal, age=12;
  char string[20];
  retVal = snprintf(string, 20, "My age: %d", age);
  if(retVal == -1) 
    printf("Could not store entire length into string\n");
  return 0;
}
TLD
 
Old 10-11-2002, 05:16 PM   #7
Ztyx
Member
 
Registered: Dec 2001
Location: Stockholm, Sweden
Distribution: Ubuntu, Kubuntu and Debian
Posts: 338

Rep: Reputation: 30
You're right. It's even better.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ string to integer conversion fatman Programming 6 06-18-2006 08:17 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration