944,208 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 76284
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 8th, 2005
0

Function to convert integer to single char.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gebbit is offline Offline
4 posts
since Jun 2005
Jun 8th, 2005
0

Re: Function to convert integer to single char.

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...
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 8th, 2005
0

Re: Function to convert integer to single char.

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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 8th, 2005
0

Re: Function to convert integer to single char.

gebbit, you can also look into sprintf, which gives you a lot of options.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jun 8th, 2005
0

Re: Function to convert integer to single char.

Thank you Dave Sinkula. Adding "+ '0'" works fine.

It took me a little to understand why exactly it works though. :o Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gebbit is offline Offline
4 posts
since Jun 2005
Jun 8th, 2005
0

Re: Function to convert integer to single char.

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gebbit is offline Offline
4 posts
since Jun 2005
Jun 8th, 2005
0

Re: Function to convert integer to single char.

you can do this:

char temp[10];
int num = 3;
sprintf( temp, "String%d", num);
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jan 21st, 2008
0

Re: Function to convert integer to single char.

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;
}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Andre79 is offline Offline
4 posts
since Jan 2008
Jan 21st, 2008
0

Re: Function to convert integer to single char.

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Andre79 is offline Offline
4 posts
since Jan 2008
Jan 21st, 2008
0

Re: Function to convert integer to single char.

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Andre79 is offline Offline
4 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: string array substitution
Next Thread in C Forum Timeline: Function to release CPU?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC