1

Okay so I don't know the exact name of it (which is why it is wrong in the title), and which is also why I don't know where to search. Can anyone tell me what its called so I can search it?

Basically its what index uses, in it being the number associated with each letter. Each letter has a specific number to along with it, along with all other non-numerical characters. For example (may be wrong)

A = 92

a = 31

B = 93

b = 32

C = 94

c = 33

And so on like that. Or, could anyone tell me how to get the number through a simple program?

  • 5
    Where do these numbers come from? They don't look like standard [ascii codes](http://www.asciitable.com/)... – assylias Jan 12 '14 at 19:59
  • This is most probably ASCII encoding of your chars. What value they have depends on which encoding you're using to represent your chars. You can cast them to an int inside C++, however be aware that it really depends exactly which chars you use on what values you will get and how reliable those are. – divinas Jan 12 '14 at 20:01

5 Answers5

4

This is very simple, just cast your char to int too see Character code:

    System.out.println('A');          // A
    System.out.println((int) 'A');    // 65

In Java character are encoded with UTF-16

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value.

Sample code to print character codes from specified string

char[] chars = "abcdefghijklmnopqrstuvwxyz01234567890....".toCharArray();

System.out.println("char  unicode   hex      code");
for (char c : chars)
    System.out.println(String.format(
            "'%s' : \\u%04x :  0x%04X : %s", c, (int) c, (int) c, (int) c));

}

Result

char  unicode   hex      code
'a' : \u0061 :  0x0061 : 97
'b' : \u0062 :  0x0062 : 98
'c' : \u0063 :  0x0063 : 99
'd' : \u0064 :  0x0064 : 100
'e' : \u0065 :  0x0065 : 101
'f' : \u0066 :  0x0066 : 102
'g' : \u0067 :  0x0067 : 103
'h' : \u0068 :  0x0068 : 104
'i' : \u0069 :  0x0069 : 105    
...
MariuszS
  • 30,646
  • 12
  • 114
  • 155
3

Cast (via automatic widening) the character to int:

char c = 'A'; // A when printed
int i = c; // 65 when printed

The int value 65 is 41 in hex or 101 in octal. I assume you have made some numerical error in your question and that you want one of these values.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Java uses Unicode, a numbering of all characters in the world. Windows and Linux have a Character Map utility (Windows, Accessories, System)-

Interpreting a char to int will often do:

String s = "Hello, world!";
for (int i = 0; i < s.length(); ++i) {
    char ch = s.charAt(i);
    System.println("Char " + ch + " has code " + (int)ch);
}
s = "ŝi estas ĉarma";
for (int i = 0; i < s.length(); ) {
    int c = s.codePointAt(i);
    int n = Character.charCount(c);
    System.println("Char " + s.substring(i, i + n) + " has code " + c);
    i += n;
}

Using codePoint is more for Asian languages.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

I think what you are looking for is ASCII (ask-ee). All (english) letters, numbers, and some punctuation characters have ASCII codes associated with them. You can find the ASCII code with

System.out.println((int)'a');

or

int a = 'a';
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
1

Your numbers are wrong for Java. You're looking for ASCII and/or Unicode; Unicode is the international superset of ASCII. Java uses the UTF-16 encoding of Unicode for its characters.

To use the numbers in a program, just cast the character to integer or vice versa

char c='A';
int i=(int)c; // i will be set to 65
keshlam
  • 7,931
  • 2
  • 19
  • 33