LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-30-2004, 08:30 AM   #1
husniteja
LQ Newbie
 
Registered: Jun 2004
Location: South Korea
Distribution: redhat
Posts: 14

Rep: Reputation: 0
how to convert charcter to integer


dear all

i want to convert some character to integer, example
Code:
char status[5];
printf("enter your status\n");
scanf("%s",status);
i need the status convert to integer so i can use it with switch ... case statement

thanks, i running in visual C++
 
Old 08-30-2004, 08:57 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
depends if your using c or c++ but as your code is in c, i'll give you the c solution, the function you want is atoi(), check out msdn or whatever documentation visual c++ has.
 
Old 08-30-2004, 08:57 AM   #3
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
use the function atoi for integer
and atol for long integer.
'man atoi' for more info.

edit: fixed typo

Last edited by Stor_G; 08-30-2004 at 08:58 AM.
 
Old 08-30-2004, 09:12 AM   #4
husniteja
LQ Newbie
 
Registered: Jun 2004
Location: South Korea
Distribution: redhat
Posts: 14

Original Poster
Rep: Reputation: 0
hi kev, thanks for this answer

but how to back it to array of charcter, i mean convert integer to array
 
Old 08-30-2004, 09:17 AM   #5
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
in order to convert an integer to a character, i use sprintf.
i do something like this:
Code:
#include <stdio.h>
char[50] arrcBuffer;

int nNum = 321;

sprintf(arrcBuffer, "%d", nNum);
hope this helps.
 
Old 08-30-2004, 09:23 AM   #6
husniteja
LQ Newbie
 
Registered: Jun 2004
Location: South Korea
Distribution: redhat
Posts: 14

Original Poster
Rep: Reputation: 0
hi stor

Thanks for your answer ^-^
 
Old 08-30-2004, 09:29 AM   #7
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
happy to be of service

just be careful not to overflow the buffer.
using sprintf, without input verification, might cause a buffer overrun bug and possibly a security threat.

so if you allocate a buffer of 50 chars, make sure that the number isn't bigger than 50 digits.
another option a bit more complicated and not necessarily better is to dynamically allocate the memory according to the size of the input.
only problem is the question of who needs to free the memory.
if that is not a problem for you, you might want to choose the latter option.

cheers.
 
  


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 how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
Convert Integer to Char gjagadish Programming 5 10-14-2005 10:09 AM
convert string to integer with python Kanaflloric Programming 2 05-27-2005 11:04 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

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

All times are GMT -5. The time now is 12:26 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