Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an ASCII char to its ASCII int value?

Tags:

c++

arduino

I would like to convert a char to its ASCII int value.

I could fill an array with all possible values and compare to that, but it doesn't seems right to me. I would like something like

char mychar = "k"
public int ASCItranslate(char c)
return c   

ASCItranslate(k) // >> Should return 107 as that is the ASCII value of 'k'.

The point is atoi() won't work here as it is for readable numbers only.

It won't do anything with spaces (ASCII 32).

like image 293
user613326 Avatar asked Apr 14 '13 12:04

user613326


People also ask

How would you convert a character to its ASCII integer code?

Like, double quotes (" ") are used to declare strings, we use single quotes (' ') to declare characters. Now, to find the ASCII value of ch , we just assign ch to an int variable ascii . Internally, Java converts the character value to an ASCII value. We can also cast the character ch to an integer using (int) .

How do you change the ASCII value of a character?

Very simple. Just cast your char as an int . char character = 'a'; int ascii = (int) character; In your case, you need to get the specific Character from the String first and then cast it.

How do I convert a char to an int in C++?

All you need to do is: int x = (int)character - 48; Or, since the character '0' has the ASCII code of 48, you can just write: int x = character - '0'; // The (int) cast is not necessary.


1 Answers

Do this:-

char mychar = 'k';
//and then
int k = (int)mychar;
like image 197
Waqar Avatar answered Sep 28 '22 10:09

Waqar