24

I have a character:

 char ch='A'

Give me method which converts char to ASCII
or way which returns a ASCII NUMBER.

Output : 65

Laksh
  • 6,717
  • 6
  • 33
  • 34
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • i suggest you read about unicode, multibyte characters, and java Character api – CrackerJack9 Sep 16 '11 at 20:22
  • 1
    There is a huge amount of characters that don't have an `ASCII` representation at all: `char` has a range of 65535 whereas `ASCII` is restricted to 128. – Saul Sep 19 '11 at 14:35

6 Answers6

42
char ch='A';

System.out.println((int)ch);
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 2
    `char` has a range of 65535 whereas `ASCII` is restricted to 128. There is a huge amount of characters that don't have an `ASCII` representation at all. Using an `int` misleads one into thinking there exists a 1-to-1 correspondence between Unicode characters and `ASCII` codes. – Saul Sep 19 '11 at 14:33
  • 5
    @Saul: Indeed. But the OP is asking how to get the ASCII code for a character, which implies that the characters he's interested in will have ASCII codes. – Oliver Charlesworth Sep 20 '11 at 08:17
  • Unicode numbers for such characters are equal to their ASCII counterparts because **Unicode is a complete superset of ASCII**. For example, `A` in Unicode is `U+0041` or `65` in decimal. In contrast, ASCII has no mapping for 99% of `char`-s. That is the reason I argue in favor of code points. – Saul Sep 20 '11 at 10:13
  • @Saul given the OP's question using a primitive `char`, it can be converted to an `int`. A more complete answer may discuss a `Character`, which a completely different story. While you edited your previous answer to include some details, you still unnecessarily instantiate a `String` object and have refused to address several comments directed to that fact. – CrackerJack9 Sep 20 '11 at 21:20
  • @CrackerJack9: Yes. And my opinion, converting to an `int` for obtaining an ASCII code is both misleading because there is no 1-to-1 mapping, and also obsolete because Unicode is a complete superset of ASCII. The reason why I argue in favor of a `String` is because a primitive `char` is restricted to *Basic Multilingual Plain* and `Character.codePointAt` does not support objects. Plus, `char[]` boils down to a `new` also (a.k.a. object instantiation). I'll be more than happy to update my answer if You show which of these reasons is either false or superfluous. – Saul Sep 21 '11 at 09:54
  • Thank's so much! Very simple and clean! – Ben Hagel Jan 04 '13 at 09:04
16

There is a major gotcha associated with getting an ASCII code of a char value.

In the proper sense, it can't be done.

It's because char has a range of 65535 whereas ASCII is restricted to 128. There is a huge amount of characters that have no ASCII representation at all.

The proper way would be to use a Unicode code point which is the standard numerical equivalent of a character in the Java universe.

Thankfully, Unicode is a complete superset of ASCII. That means Unicode numbers for Latin characters are equal to their ASCII counterparts. For example, A in Unicode is U+0041 or 65 in decimal. In contrast, ASCII has no mapping for 99% of char-s. Long story short:

char ch = 'A';
int cp = String.valueOf(ch).codePointAt(0);

Furthermore, a 16-bit primitive char actually represents a code unit, not a character and is thus restricted to Basic Multilingual Plane, for historical reasons. Entities beyond it require Character objects which deal away with the fixed bit-length limitation.

Saul
  • 17,973
  • 8
  • 64
  • 88
  • String.valueOf is heavy. This is a very convoluted way – njzk2 Sep 16 '11 at 12:19
  • However, this is an interesting way of getting the unicode codepoint rather than the value relatively to the encoding. – njzk2 Sep 16 '11 at 12:21
  • why are you using String.valueOf? already have the char, just use that :) – CrackerJack9 Sep 16 '11 at 20:20
  • As i see it, the code point is different from the char value. (which is an encoding-dependent representation of the code point) – njzk2 Sep 19 '11 at 09:36
  • 1
    @CrackerJack9: Because the conceptual equivalent of an "ASCII" value in the Java universe is a Unicode code point. In contrast, a primitive `char` contains an encoded representation of a code point which is something entirely different. – Saul Sep 19 '11 at 12:57
  • @Saul First, ASCII is ASCII - it doesn't matter which 'universe' you are in. My comment was directed towards you turning a `char` into a `String` - an unnecessary conversion. – CrackerJack9 Sep 19 '11 at 17:49
  • @CrackerJack9: I suppose You have no idea what a 1-to-1 correspondence means? – Saul Sep 20 '11 at 07:46
  • @saul: I would have said that Character.codePointAt(new char[]{ch}, 0) would do the same without allocating a String. (which is what i mean by convoluted), but it seems it is more complex than that – njzk2 Sep 20 '11 at 14:56
  • @njzk2: It is. `Character.codePointAt` doesn't support `Character` objects whereas `String.valueOf` does. – Saul Sep 20 '11 at 16:34
  • @Saul my comment is along the same lines as njzk2's. `Character.codePointAt` is a `public static` method which takes a `char[]`, so it can be simply used given that we already have a `char` - just as njzk2's example shows - which does not require creating a `String` object - which is unnecessary. I will ignore your inflammatory remark, but I don't think it really has a place here. I'm merely trying to get to the best answer for anyone else's sake that stumbles upon your code example. In the future, please reply to what is being said. – CrackerJack9 Sep 20 '11 at 21:16
  • @CrackerJack9: The point is that `Character.codePointAt` doesn't support `Character` objects whereas `String` does. Object support is a must because **the primitive `char` is restricted to *Basic Multilingual Plane*** which contains only a half of Unicode characters. **Not to mention the fact that creating a `char[]` array boils down to a `new` also.** – Saul Sep 21 '11 at 09:30
4

A char is actually a numeric datatype - you can add one to an int, for example. It's an unsigned short (16 bits). In that regard you can just cast the output to, say, an int to get the numeric value.

However you need to think a little more about what it is you're asking - not all characters are ASCII values. What output do you expect for â, for example, or ?

Moreover, why do you want this? In this day and age you ought to be thinking about Unicode, not ASCII. If you let people know what your goal is, and how you intend to use this returned value, we can almost certainly let you know of a better way to achieve it.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
2
char a='a';
char A='A';
System.out.println((int)a +" "+(int)A);

Output:
97 65
2

char (with a lower-case c) is a numeric type. It already holds the ascii value of the char. Just cast it to an integer to display it as a numeric value rather than a textual value:

System.out.println("char " + ch + " has the following value : " + (int) ch);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

simply do

int a = ch

(also this has nothing to do with android)

njzk2
  • 38,969
  • 7
  • 69
  • 107