Function to convert integer to single char.

Thread Solved
Reply

Join Date: Jun 2005
Posts: 4
Reputation: gebbit is an unknown quantity at this point 
Solved Threads: 0
gebbit gebbit is offline Offline
Newbie Poster

Function to convert integer to single char.

 
0
  #1
Jun 8th, 2005
Hello, I'm new to C and having this problem. What I need to do is add a single integer (0-9) to an (obviously char) string. More specifically, I want to add the value of a function that returns an integer (0-9) to a string. I don't want to change the integer value in any way, just to convert it to a form in which I could add it to a string.

I have checked out the itoa() function, but apparently it only stores the converted integer into a whole new string, not a single subscript/element of a string as I need.

Also, the function should return as a value the integer in the char form that it needs to be in order for it to be stored in the string.

"Pseudocode" example: TheString[i]=ConvertToInt(FunctionThatReturnsInt());

Does a function like this exist in C? (If not, I guess I will have to code it, but I'd hate to "re-invent the wheel", so I am asking ) Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Function to convert integer to single char.

 
0
  #2
Jun 8th, 2005
I'm not a C guy by any means, but I think you can cast it.... such as:
  1. newvar = (int)x + (int)y;

Someone who Codes a lot of C, please check this over...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,303
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 226
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Function to convert integer to single char.

 
0
  #3
Jun 8th, 2005
For a value 0-9, you can just add '0' to get the corresponding digit character.
#include <stdio.h>

int main(void)
{
   char text[] = "StringX";
   int digit;
   for (digit = 0; digit < 10; ++digit)
   {
      text[6] = digit + '0';
      puts(text);
   }
   return 0;
}

/* my output
String0
String1
String2
String3
String4
String5
String6
String7
String8
String9
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Function to convert integer to single char.

 
0
  #4
Jun 8th, 2005
gebbit, you can also look into sprintf, which gives you a lot of options.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: gebbit is an unknown quantity at this point 
Solved Threads: 0
gebbit gebbit is offline Offline
Newbie Poster

Re: Function to convert integer to single char.

 
0
  #5
Jun 8th, 2005
Thank you Dave Sinkula. Adding "+ '0'" works fine.

It took me a little to understand why exactly it works though. :o Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: gebbit is an unknown quantity at this point 
Solved Threads: 0
gebbit gebbit is offline Offline
Newbie Poster

Re: Function to convert integer to single char.

 
0
  #6
Jun 8th, 2005
Originally Posted by winbatch
gebbit, you can also look into sprintf, which gives you a lot of options.
If I'm not mistaken, sprintf() prints to whole strings, not to subscripts of strings, which is what I needed.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Function to convert integer to single char.

 
0
  #7
Jun 8th, 2005
you can do this:

char temp[10];
int num = 3;
sprintf( temp, "String%d", num);
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: Andre79 is an unknown quantity at this point 
Solved Threads: 1
Andre79 Andre79 is offline Offline
Newbie Poster

Re: Function to convert integer to single char.

 
0
  #8
Jan 21st, 2008
man its easy u have to use ascii code i will show u how with a simple program ok i hope this could help you and pls send me a reply telling me if thats what ur looking for :this silly software works o 0 to 9;

#include<iostream.h>
int main ()
{
int a;

cout<<"enter nnumber";
cin>>a;

char x;
x=a+48;
cout<<x<<endl;

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: Andre79 is an unknown quantity at this point 
Solved Threads: 1
Andre79 Andre79 is offline Offline
Newbie Poster

Re: Function to convert integer to single char.

 
0
  #9
Jan 21st, 2008
when u put 0 in variable a , x holds a+48 this is the simple formula
to do a large number like 1234...etc u have to devide the number and put it in an array then switch each array element to type char and they put them in a char element or u just cout them ur welcome
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: Andre79 is an unknown quantity at this point 
Solved Threads: 1
Andre79 Andre79 is offline Offline
Newbie Poster

Re: Function to convert integer to single char.

 
0
  #10
Jan 21st, 2008
if ur looking for a function i will post it, it is also 1 interger from 0 to 9 i will do later a small program for larger integers:
  1. #include<iostream.h>
  2.  
  3. char integer_to_char(int);//prototype
  4.  
  5. int main ()//function main starts
  6. {
  7. int a;//varibale that will hold the integer value
  8.  
  9. cout<<"enter nnumber";//msg that prompt the user
  10. cin>>a;//user input
  11.  
  12. cout<<"the number represented in char form is:"<<integer_to_char(a)<<endl;//calling the function
  13. return 0;
  14. }
  15. //function program
  16. char integer_to_char(int x)
  17. {
  18. char y;
  19. y=x+48;
  20. return y;
  21. }
Last edited by WolfPack; Jan 21st, 2008 at 11:50 am. Reason: added [code=c][/code] tags. Next time use them when posting code.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC