Solved

Changing String To Integer and Integer to String

Posted on 2003-11-19
14
12,571 Views
Last Modified: 2013-11-20
How can i Change Integer to String and String to Integer by using this function?

int strtoint(char ch);
char inttostr(int no);

anybody can help me with this? what should i use for the "Include" header?
0
Comment
Question by:girlswants_me
  • 5
  • 4
  • 4
  • +1
14 Comments
 

Author Comment

by:girlswants_me
ID: 9776944
anybody?
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9776984
string to integer
============

int nVal = atoi(szSTR);


integer to string
============

char szSTR[10];
itoa(nVal, szSTR, 10);

Rosh :)
0
 

Author Comment

by:girlswants_me
ID: 9777005
can you give me a complete function for that please.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9777007
#include "Shlwapi.h"

Project Menu->Settings
Link Tab->

Object/Library Modules ->

"Shlwapi.lib"   <-----Add this in that edit box
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9777031
Now u can Use the functions StrToInt, IntToStr
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9777035
oops IntToStr  is not in that lib.
0
 

Author Comment

by:girlswants_me
ID: 9777164
Sorry i cannot give you points. Can you create a function by using what is stipulated above?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9777196
#include <stdlib.h>


int strtoint(char* pch)
{
       return itoa(pch);
}


char* inttostr(int no, char* pch)
{
      return itoa(no, pch, 10);
}

Is that what you want...?
Rosh :)
Rosh :)
0
 

Expert Comment

by:merlic
ID: 9785377
#include <stdlib.h>


int strtoint(char* pch)
{
       return atoi(pch);   // atoi function, not itoa!!
}


char* inttostr(int no, char* pch)
{
      return itoa(no, pch, 10);
}
0
 

Expert Comment

by:merlic
ID: 9785434
//----------------- include ---------------------------------------

#include <stdlib.h>

//----------------- declarations ---------------------------------

int strtoint(char* pch);
char* inttostr(int no, char* pch);

//----------------- definitions -----------------------------------

int strtoint(char* pch)
{
       return atoi(pch);   // atoi function, not itoa!!
}


char* inttostr(int no, char* pch)
{
      return itoa(no, pch, 10);
}

hi,
merlic
0
 

Accepted Solution

by:
merlic earned 35 total points
ID: 9785496
try this if you need only one parameter passed to the functions

//----------------- include ---------------------------------------

#include <stdlib.h>

//----------------- declarations ---------------------------------

int strtoint(char* pch);
char* inttostr(int no); //only the int parameter

//----------------- definitions -----------------------------------

int strtoint(char* pch)
{
       return atoi(pch);   // atoi function, not itoa!!
}


char* inttostr(int no) // only the int parameter
{
      char pch[10] = "";
      return itoa(no, pch, 10);
}

hi,
merlic
0
 

Author Comment

by:girlswants_me
ID: 9793399
thats more like it. Thank you very much merlic.
0
 

Expert Comment

by:merlic
ID: 9795267
nice to help you!
bye
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 11171711
Why is this in the "time tested" solutions?  For most systems out there, it is technically incorrect:

1: a 32-bit "int" value, when converted to a string can occupy 10 character places; so a buffer of only 10 chars is incorrect: there is no room allocated for the terminating NUL (not NULL) character.

2: The accepted answer basically returns the address of a non-static local variable, which is incorrect.  It might seem to work, but that does not mean it is correct.

Peace!

-=- James.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 49
how to monitor remote shell execution on linux 9 92
zeroMAx challenge 20 76
Path of Workbook 3 45
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now