How do I convert an int to a char and also back from char to int?
e.g 12345 == abcde
Right now I have it using a whole bunch of case statement, wonder if there is a smarter way of doing that?
Thanks,
Tee
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would recommend use ASCII values and just typecast.
In most cases it is best to just use the ASCII values to encode letters; however if you wanted to use 1 2 3 4 to represent ‘a’ ‘b’ ‘c’ ‘d’ then you could use the following.
For example, if you wanted to convert the letter 1 to ‘a’ you could do:
as in ASCII 97 corresponds to the character ‘a’. Likewise you can convert the character ‘a’ to the integer 1 as follows
Of course it is just easier to use ASCII values to start with and avoid adding or subtracting as shown above. 😀