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"
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"
Read up on stringstreams.
..Here is an example:
Code:std::string convert(int n) { std::ostringstream os; os<<n; return os.str(); }
Basically this.
int d;
static_cast<char>(d)
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.
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?
How did you determine the conversion?Originally Posted by Swoorup
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
http://www.dolcevie.com/js/converter.html
I also studied about low level programming.
EDIT: HELP!!!
Last edited by Swoorup; 11-27-2011 at 08:36 AM.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Last edited by Swoorup; 11-27-2011 at 08:45 AM.
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.Originally Posted by Swoorup
Anyway, what is your question now?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by Swoorup
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-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; }
Let's return to your question:
In other words, you want to convert an integer to a string. Maybe the integer is an int, or a long, or some other integer type, but it is an integer nonetheless. Maybe the string is a std::string, a null terminated string, or some other string type, but it is a string nonetheless.Originally Posted by Swoorup
Now take a look at your function:
I don't know what is byte, but your function clearly does not solve your problem. I would expect something like this:Code:byte byteTochar(byte anyByte,byte strPos)
Code:std::string intToString(int number)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)