0

I am trying to translate some C# code into java and would like to know what is the java equivalent of System.Convert.ToInt32(char):

Converts the value of the specified Unicode character to the equivalent 32-bit signed integer.

Convert.ToInt32(letter);  
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
DD.
  • 21,498
  • 52
  • 157
  • 246
  • If you are referring to `letter` as a `char` instead of `String` per Tim's edit, read [this](http://stackoverflow.com/questions/4968323/java-parse-int-value-from-a-char). Either way, google the question before asking. These are found within seconds. – Jeroen Vannevel Aug 31 '14 at 10:53
  • @JeroenVannevel: `Integer.parseInt(char)` is not the same as `Convert.ToInt2(char)` for the same reason as why this implcicit conversion `int charNum = '5';` yields 53 not 5. – Tim Schmelter Aug 31 '14 at 10:59
  • @TimSchmelter: I'm aware. Prior to your edit I assumed he referred to `Convert.ToInt32(string)`. In my comment I point to an approach that parses chars using `Character.getNumericValue`. – Jeroen Vannevel Aug 31 '14 at 11:00
  • 1
    "(int)letter" will give the same result as "Convert.ToInt32(letter)". – Dave Doknjas Aug 31 '14 at 14:23

1 Answers1

5

"Convert.ToInt32(someChar)" does exactly what "(int)someChar" does. Since "(int)someChar" is available in Java, why not use that?

When testing the various options, use '5' as a test - some options will convert this simply to the integer 5, but you will want the integer 53 to match the original C# behavior of Convert.ToInt32.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28