converting int to string...

View previous topic View next topic Go down

CPP converting int to string...

Post by bit 02 26$ rayhan on Wed Feb 09, 2011 2:14 am

How I can convert a int to a string?
in C


Last edited by BIT0102-Mohaimin on Sat Feb 12, 2011 9:41 pm; edited 1 time in total (Reason for editing : the title was "converting int to string.................", changed because too many dots are unnecessary and should not be used in title.)

bit 02 26$ rayhan
Pre-Alpha Release
Pre-Alpha Release

Posts : 5
Points : 9

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by BIT0216-Habib on Wed Feb 09, 2011 2:43 am

its very good news Clapping that many of the student from 2nd batch student are logging in to our forum. I want to say them that this is your forum. Make best use of it to get benefit from it as this forum has some great specialists to solve your problems. you also can be like them i hope.

Not only for help you have more sections to enjoy and share information.

Again thanks to them who helps to make this forum enjoyable.

N.B: wait you will get help within short time.
avatar
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by BIT0101-rayhan on Wed Feb 09, 2011 3:12 am

Code:

#include <stdlib.h>   
#include <stdio.h>   

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

   
   itoa(num, array, 10);

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

   return 0;
}
avatar
BIT0101-rayhan
Release Candidate
Release Candidate

Posts : 107
Points : 177

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by bit 02 26$ rayhan on Wed Feb 09, 2011 3:36 am

I did it in the first time but my compiler codeblocks and gcc didn't support itoa() method.
I used the stdlib
What can I do now?
@BIT0101-rayhan

bit 02 26$ rayhan
Pre-Alpha Release
Pre-Alpha Release

Posts : 5
Points : 9

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by BIT0101-rayhan on Wed Feb 09, 2011 4:19 am

i think u have got a problem with ur ide or compiler. By the way, it is a non standard c library function
avatar
BIT0101-rayhan
Release Candidate
Release Candidate

Posts : 107
Points : 177

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by BIT0101-rayhan on Wed Feb 09, 2011 4:24 am

well, there is a hard way:
12345/10000 = 1
12345/1000%10 = 2
12345/100%10 = 3
...
...
by this way, u can parse every digit and then put it into the character array
avatar
BIT0101-rayhan
Release Candidate
Release Candidate

Posts : 107
Points : 177

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by BIT0130-Shakkhor on Wed Feb 09, 2011 4:43 am

There's simple way. There's a method sprintf() in stdio.h that works exactly like the printf(), except it prints the output onto a char array instead of stdout. The return is the lenght of the array. Here's an example:
Code:
int main ()
{
  char str[50];
  int  a=5, length;
  length=sprintf(str, "%d", a);
  return 0;
}
You should be able to convert pretty much anything (this excludes stuff like combs, washing machines or your pendrive, they may not work and I'm not to blame) into string with this one.

_________________________________________________________________
I'm not a driver with drinking problem.
I'm an alcoholic with driving problem.

Blog | বাংলা ব্লগ
avatar
BIT0130-Shakkhor
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 209
Points : 328

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by BIT0102-Mohaimin on Wed Feb 09, 2011 1:41 pm

I use the method Shakkhor mentioned.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

CPP Re: converting int to string...

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum