goldsoft
goldsoft

Reputation: 679

how to convert char to int in java for android

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

Upvotes: 1

Views: 8686

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499770

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
  • [...]

Upvotes: 5

Related Questions