![]() |
| ||
| 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. |
| ||
| 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: newvar = (int)x + (int)y; Someone who Codes a lot of C, please check this over... |
| ||
| 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> |
| ||
| Re: Function to convert integer to single char. gebbit, you can also look into sprintf, which gives you a lot of options. |
| ||
| 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. :) |
| ||
| Re: Function to convert integer to single char. Quote:
|
| ||
| Re: Function to convert integer to single char. you can do this: char temp[10]; int num = 3; sprintf( temp, "String%d", num); |
| ||
| 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; } |
| ||
| 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:D |
| ||
| 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: #include<iostream.h> |
| All times are GMT -4. The time now is 12:36 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC