1

how to convert char to int in java for android

for example:

int MyInt;
char MyChar = 'A';

how to insert the value 65 into MyInt ?

in C# it like: MyInt = Convert.ToChar(MyChar);

thanks

1 Answer 1

5

You can do it the same way in both C# and Java:

char myChar = 'A';
int myInt = myChar;

I wouldn't use Convert.ToInt32 (which is what I assume you meant) in C#... I'd just use the implicit conversion shown above.

EDIT: In Java, this uses the implicit widening primitive conversion specified in section 5.1.2 of the Java Language Specification:

The following 19 specific conversions on primitive types are called the widening primitive conversions:

  • byte to short, int, long, float, or double
  • short to int, long, float, or double
  • char to int, long, float, or double
  • [...]
Sign up to request clarification or add additional context in comments.

2 Comments

Thenks for the help, and how to convert string to char ?
@goldsoft: You can get a single character from a string using String.charAt. Obviously if the string has more than one character, you need to decide which one you mean...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.