# Thread: Convert integer to character

1. ## Convert integer to character

How can I convert an integer to characters value?

For instance there is a number from 0 to 64
Then the
 Integer Character equivalent 0 "00" 1 "01" 2 "02" 3 "03" 4 "04" 5 "05" 6 "06" 7 "07" 10 "10" 11 "11"

..Here is an example:
Code:
```std::string convert(int n)
{
std::ostringstream os;
os<<n;
return os.str();
}```

3. Basically this.

int d;
static_cast<char>(d)

4. Originally Posted by whiteflags
Basically this.

int d;
static_cast<char>(d)
How would that turn 11 into "11" ?

5. Do you want a string or a character? If you really want a character, my answer is correct. If you really want a string, your answer is correct, manasij.

6. Ok conjuring up my own findings, Actually I decided that a instead of integer a byte is to be converted to 2 characters.
Suppose we have 0x01 as a byte then the string equivalent would be 0x3031
For 63 number, the conversion would be 0x3F then the string equivalent would be 0x3633.

How can I get that done?

7. Originally Posted by Swoorup
Actually I decided that a instead of integer a byte is to be converted to 2 characters.
Suppose we have 0x01 as a byte then the string equivalent would be 0x3031
For 63 number, the conversion would be 0x3F then the string equivalent would be 0x3633.
How did you determine the conversion?

8. http://www.dolcevie.com/js/converter.html

I
also studied about low level programming.

EDIT: HELP!!!

9. Sorry, but there is no algorithm on that page (except maybe if you view source). How did you determine the conversion?

If all you did was to put some text into an online converter and get the result, then you need to figure out what that online converter is doing, then implement an algorithm to do the same thing.

10. No, I did not mean that.
The online converter just inputs string characters not integers or byte characters.

I just know about how strings, floating points, long, bytes gets laid out in the memory.

EDIT:
Suppose you have two characters "05"
Then we know that byte equivalent of "0" is 0x30 and for "5" 0x30+0x5=0x35

So the memory stores the characters this way: 0x30,0x35

EDIT: I just want to convert a byte to two characters.

11. Originally Posted by Swoorup
Suppose you have two characters "05"
Then we know that byte equivalent of "0" is 0x30 and for "5" 0x30+0x5=0x35
You can also say that the 'byte equivalent of "0"' is '0', or assuming ASCII, 48. There is nothing special about using hexadecimal notation, other than it being convenient when we want to deal with bytes.

Anyway, what is your question now?

12. So how would you convert 10 to "10", when in the memory its laid out as 0x0A, how to make it 0x3031?????

13. Originally Posted by Swoorup
So how would you convert 10 to "10", when in the memory its laid out as 0x0A, how to make it 0x3031?????
Refer to manasij7479's post #2.

14. Ok since I could consider the string as a whole to be reliable variable, where I'd just want two characters I made my own function.
Please if there can be room for improvement please tell me. It works for values from 00 to 99 only though
Code:
```byte byteTochar(byte anyByte,byte strPos){
byte character=0;

switch (strPos)
{
case 1:
if (anyByte>9){character=anyByte/10;}
break;
case 2:
if (anyByte>9){character=10%anyByte;}else{character=anyByte;}
break;
default:
exit(0);//
break;
}
character+=48;
printf("%d",character);
return character;
}```

`byte byteTochar(byte anyByte,byte strPos)`
`std::string intToString(int number)`