954,113 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Function to convert integer to single char.

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.

gebbit
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

I'm not a C guy by any means, but I think you can cast it.... such as:

newvar = (int)x + (int)y;


Someone who Codes a lot of C, please check this over...

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

gebbit, you can also look into sprintf, which gives you a lot of options.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

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

It took me a little to understand why exactly it works though. :o Thanks again. :)

gebbit
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
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.

gebbit
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

you can do this:

char temp[10];
int num = 3;
sprintf( temp, "String%d", num);

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

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
int main ()
{
int a;

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

char x;
x=a+48;
cout<

Andre79
Newbie Poster
4 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

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:D

Andre79
Newbie Poster
4 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

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:

#include<iostream.h>

char integer_to_char(int);//prototype

int main ()//function main starts
{
	int a;//varibale that will hold the integer value
	
	cout<<"enter nnumber";//msg that prompt the user
		cin>>a;//user input

cout<<"the number represented in char form is:"<<integer_to_char(a)<<endl;//calling the function 
		return 0;
}
//function program
char integer_to_char(int x)
{
char y;
y=x+48;
return y;
}
Andre79
Newbie Poster
4 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

In case you didn't notice, the fellow wanted the code in C.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

> In case you didn't notice, the fellow wanted the code in C.
He also wanted it 3 YEARS ago as well.

Not only late, and in the wrong language, it's also old C++, and non-portable.
Use '0', not 48 (like was used oh so long ago in a galaxy far far away).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

hello dear brothers
the following program is not real result for me
please explain me
thanks a lot

#include
int main ()
{
int a;

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

char x;
x=a+48;
cout<

themoon49
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You