1

I need to get the ANSI value of a char in java

Ive tried casting my char to an int type but it returns a value that is not its ANSI value.

is there any way to do this?

one letter in hebrew which is "ל" has the ansi value of 236 but when i cast it to int i get 1500

Itay S
  • 73
  • 7

2 Answers2

6

What you call "ANSI" is actually the Windows codepage 1255 character set (Windows-1255). It's the Hebrew codepage from Microsoft.

You can get your character value in that encoding using the following code:

public static void main(String[] args) throws UnsupportedEncodingException {
    String s = "ל";
    byte[] b = s.getBytes("Windows-1255");
    System.out.println(b[0] & 0xff);
}

This prints:

236

(Note: this also works on other operating systems than Windows, such as MacOS)

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
-2
char[] chars = {'&','a'};
for(int i =0; i<chars.length; i++)
System.out.println(Character.codePointAt(chars,i));

I think the value that you are referring to is the codepoint of the character. There are many ways to do this which you can find on the documentation page of java about Characters.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459