0

How am I to convert a char into its numerical encoding value?

The code I used is

public class Q1A {
    int toInt(char character){
        int getInt = toInt(character);
        return getInt;
    }
}

Sorry if my question is hard to understand

Kieran
  • 43
  • 3

4 Answers4

1

you just need

int toInt(char character){
   int  i = (int) character; // this will gives int value(ASCII) of char.
   return i;
}

Or you can simplify to

int toInt(char character){
   return (int) character;      
}

You can use Character.getNumericValue(c) too to get numeric value . More about Character.getNumericValue(c)

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

It depends on if you want to actual numeric value of the char or the ascii value or somthing similar

//Get numeric value
int toInt(char c)
{
   return Character.getNumericValue(c);
}

//Get ascii value
int toInt(char c)
{
   return (int)c;
}
string.Empty
  • 10,393
  • 4
  • 39
  • 67
0

Do you mean the ASCII value of a char?

int toInt(char character) {
    return (int)character
}
0

You can use java Character class

Character.getNumericValue(char)

Sumit Jain
  • 87
  • 9