Kieran
Kieran

Reputation: 43

Java character conversion to numerical encoding

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

Upvotes: 0

Views: 224

Answers (4)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

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)

Upvotes: 1

Sumit Jain
Sumit Jain

Reputation: 97

You can use java Character class

Character.getNumericValue(char)

Upvotes: 0

Do you mean the ASCII value of a char?

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

Upvotes: 0

Nicolas Tyler
Nicolas Tyler

Reputation: 10552

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;
}

Upvotes: 0

Related Questions