String buffer = "c";
char ch = buffer.charAt(0);
System.out.println((int)ch);
This should do it.
Answer from Ankur on Stack OverflowHow do I get the decimal value of a unicode character in Java? - Stack Overflow
Java printing String characters as unicode decimal value.
java - How to convert char to decimal using an ascii table? - Stack Overflow
Convert special characters into decimal equivalents in java - Stack Overflow
int a = 1;
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 1 (start-of-heading char, which isn't printable; see this table: C0 Controls and Basic Latin, same as ASCII)
int a = '1';
char b = (char) a;
System.out.println(b);
will print out the char with Unicode code point 49 (one corresponding to '1')
If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);.
If you want to convert an int seen as a Unicode code point, you can use Character.toChars(48) for example.
My answer is similar to jh314's answer but I'll explain a little deeper.
What you should do in this case is:
int a = 1;
char b = (char)(a + '0');
System.out.println(b);
Here, we used '0' because chars are actually represented by ASCII values. '0' is a char and represented by the value of 48.
We typed (a + '0') and in order to add these up, Java converted '0' to its ASCII value which is 48 and a is 1 so the sum is 49. Then what we did is:
(char)(49)
We casted int to char. ASCII equivalent of 49 is '1'. You can convert any digit to char this way and is smarter and better way than using .toString() method and then subtracting the digit by .charAt() method.
I suspect you're just interested in a conversion from char to int, which is implicit:
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
int value = c;
System.out.println(value);
}
EDIT: If you want to handle surrogate pairs, you can use something like:
for (int i = 0; i < text.length(); i++)
{
int codePoint = text.codePointAt(i);
// Skip over the second char in a surrogate pair
if (codePoint > 0xffff)
{
i++;
}
System.out.println(codePoint);
}
Ok after reading Jon's post and still musing about surrogates in Java, I decided to be a bit less lazy and google it up. There's actually support for surrogates in the Character class it's just a bit.. roundabout
So here's the code that'll work correctly, assuming valid input:
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isHighSurrogate(ch)) {
System.out.println("Codepoint: " +
Character.toCodePoint(ch, str.charAt(i + 1)));
i++;
}
System.out.println("Codepoint: " + (int)ch);
}
I put a word into a string array and now I'm trying to print the letters as their unicode decimal values.
Scanner so = new Scanner(System.in);
String word = so.nextLine();
String[] chars = word.split("");
System.out.println(chars[0]);
So the code should print out the first letter of the word, but I want to convert it to its unicode decimal value. I've been scouring the internet for the last hour but nothing is working. does anyone have any answers? thanks!
The ascii (char) value of 0 is 48 and the value if 1 is 49,
so you need to subtract 48 from the value
a = binaryNumber.charAt(0);
int aInt = (a - 48) * 2 * 2* 2;
....
System.out.println(binaryNumber + " in decimal is: " + (aInt + bInt + cInt + dInt));
The problem is you are printing the a b c and d as chars so it will print what ever decimal value of a b c and d correspond to in the ascii table. If you want to print out decimals you will have to convert the value to decimal by subtracting 48 add them together and then print.
Has to be like this: 1010 = 8 + 0 + 2 + 0 = 10 then print 10. You are on the right track
This can be simply achieved with String.format(). The representations are simply the character value as decimal, padded to 4 characters and wrapped in &#;
The only tricky part is deciding which characters are "special". Here I've assumed not digit, not whitespace and not alpha...
StringBuilder output = new StringBuilder();
String input = "Foo bar baz";
for (char each : input.toCharArray()) {
if (Character.isAlphabetic(each) || Character.isDigit(each) || Character.isWhitespace(each)) {
output.append(each);
} else {
output.append(String.format("&#%04d;", (int) each));
}
}
System.out.println(output.toString());
You just need to fetch the integer value of the character as mentioned in How do I get the decimal value of a unicode character in Java?.
As per Oracle Java doc
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Assuming your characters fall within the character range, you can just get the decimal equivalent of each character from your string.
String text = "";
char[] cArr = text.toCharArray();
for (char c : cArr)
{
int value = c; // get the decimal equivalent of the character
String result = "& #" + value; // append to some format string
System.out.println(result);
}
Output:
& #169
& #8482
& #174
I am trying to convert a char value to its corresponding integer for example '0x21A6' is the same as integer 6. Here is my code:
public String toIntegersString(char[] arrows){
//an empty string to store the converted integer values
String ints = " ";
for (int j = 0; j< arrows.length; j ++) {
//the method I used to convert to integers
int nums = Character.getNumericValue(arrows[j]);
// converting from integer to String
ints = ints + String.valueOf(nums) + " ";
}
return ints;
}
Then my main in my main method this is what I have:
ArrowSymbol arrs = new ArrowSymbol();
//This is to just be able to connect to my helper class
char[] arrows = { 0x21A6};
System.out.println(arrs.toIntegersString(arrows));
My problem is when I do this, it prints out -1 meaning it is out of bounds. Please assist
The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.
So you can get the int value of a decimal digit char by subtracting '0'.
char x = '9';
int y = x - '0'; // gives the int value 9
I you have the char '9', it will store its ASCII code, so to get the int value, you have 2 ways
char x = '9';
int y = Character.getNumericValue(x); //use a existing function
System.out.println(y + " " + (y + 1)); // 9 10
or
char x = '9';
int y = x - '0'; // substract '0' code to get the difference
System.out.println(y + " " + (y + 1)); // 9 10
And it fact, this works also :
char x = 9;
System.out.println(">" + x + "<"); //> < prints a horizontal tab
int y = (int) x;
System.out.println(y + " " + (y + 1)); //9 10
You store the 9 code, which corresponds to a horizontal tab (you can see when print as String, bu you can also use it as int as you see above