Char to int and back again - java

I want to find an integer representation of a character, then later on find a character representation of an int.
My current solution is this, but it doesn't work:
String s = "A"
Integer b = Character.getNumericValue(s.toCharArray()[0]); // 10
char c = Character.toChars(b)[0]; // (blank)
How should i do this?

There's a misunderstanding in how Character.getNumericValue(char) works.
Returns the int value that the specified Unicode character represents. For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
from the java docs.
In other words:
The method getNumericValue does not produce the UTF-16 value of the specified character, but attempts to produce a numeric value - in the sense that '0' will produce 0 - from the given value. And 'A' corresponds to 10 in hex.
But 10 is not the UTF16 value corresponding to 'A'.
A correct way of converting a char to it's corresponding UTF16-value would be to use one of the codepoint-methods from Character. Or if you're absolutely positive, all values will be of the BMP, you can use
char c = 'A';
int i = (int) c;
In this case Character.toChars(int) will also work.

int b = s.charAt(0); // 65
char c = (char) b // 'A'

Related

Converting int to String and then String to int array

I've been trying to do this simple program where I need to check which digit of an integer number is the biggest one. So my initial thought was that I shall convert it all into an array and go with the for loop to check which element of an array is biggest. In order to do this I converted integer into String and then characters of the String into elements of an array. I ran into problem and I looked online for solution. What I don't understand is why do I need the " temp.charAt(i) - '0'" part in order to store characters of String as elements of the array. Why can't it be only arrTemp[i] = temp.charAt(i), without the "- '0'" part.
String temp = Integer.toString(n);
int arrTemp[] = new int[temp.length()];
int max = arrTemp[0];
for(int i = 0; i<temp.length(); i++) {
arrTemp[i] = temp.charAt(i) - '0';
'0' is the character '0', not the integer 0. Characters are represented using int values (ascii code).
The ascii code for '0' is the integer value 48.
int n = (int) '0'; //n would be 48.
So, to convert '0' to the integer 0, you need to subtract '0'-'0' = 48-48 = 0
To convert '9' to int 9:
'9' - '0' = 57-48 = 9
See ASCII code https://en.wikipedia.org/wiki/ASCII
In many programming languages(Java, C etc.), a character variable does not contain a character value itself rather the ascii value of the character variable. The ascii value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127(in decimal number system). For example, the ascii value of 'A' is 65 and '0' is 48.
You can convert any digit character to integer by subtracting '0' from that character. This subtraction between characters is possible because of ascii values.
Imagine you want to convert digit character '9' to an integer 9. ASCII value of character '9' is 57 and ASCII value of character '0' is 48. Now, if you perform a subtraction operation of characters, '9' - '0', it will be treated like 57 - 48 and thus will give you a result of 9. This subtraction operation of characters converted a character '9' to an Integer 9.
Try this code example:
class Converter {
public static void main(String[] args) {
System.out.println('9'-'0');
}
}
You can see all of the characters points to an ascii value,

Java int value type to Character

I am new to java and and working on a crud calculator that takes input and holds it in an ArrayList to perform the calculations.
I am trying to add two values in an ArrayList<Character> and then replace the "+" with the sum.
if(listEqu.contains('+')) {
while(listEqu.indexOf('+') > -1) {
int plus = listEqu.indexOf('+');
int prev = listEqu.get(plus-1);
int nxt = listEqu.get(plus+1);
Character sum = (char) (nxt + prev);
listEqu.set(plus, sum);
System.out.println(listEqu);
}
}
When the input is 1+1, this returns [1, b, 1].
What I want is to return [1, 2, 1] .
Any advice? Thanks!
The problem is actually that adding two characters doesn't do what you expect.
The value of '1' + '1' is 'b'. If you want the next digit after '1' you add the integer 1 to it; i.e. '1' + 1 is '2'.
For a deeper understanding, you need to understand how character data is represented in Java.
Each char value in Java is an unsigned 16 bit integer that corresponds to a code point (or character code) in the Unicode basic plane. The first 128 of these code points (0 to 127) correspond to a characters in the old ASCII character set. In ASCII the codes that represent digits are 48 (for '0') through to 39 (for '9'). And the lowercase letters are 97 (for 'a') through to 122 (for 'z').
So as you can see, '1' + '1' -> 49 + 49 -> 98 -> 'b'.
(In fact there is a lot more to it than this. Not all char values represent real characters, and some Unicode code-points require two char values. But this is way beyond the scope of your question.)
How could I specify addition of numbers instead of addition of the characters?
You convert the character (digit) to a number, perform the arithmetic, and convert the result back to a character.
Read the javadoc for the Character class; e.g. the methods Character.digit and Character.forDigit.
Note that this only works while the numbers remain in the range 0 through 9. For a number outside of that range, the character representation consists of two or more characters. For those you should be using String rather than char. (A String also copes with the 1 digit case too ...)
Few things that can be improved with your code :
Converting the characters 1 into equivalent integer value:
int prev = Integer.parseInt(String.valueOf(listEqu.get(plus-1)));
int nxt = Integer.parseInt(String.valueOf(listEqu.get(plus+1)));
// Note : int prev = listEqu.get(plus-1) would store an ascii value of `1` to prev value i.e 49
And then converting the sum of those two values into Character back to be added to the list using Character.forDigit as:
Character sum = Character.forDigit(nxt+prev,10);
// Note Character sum = (char) (nxt + prev); is inconvertible
// and char sum = (char) (nxt + prev); would store character with ascii value 98(49+49) in your case 'b' to sum
you should first convert your prevand nxt to int value and then add them together like follow:
if(listEqu.contains('+')) {
while(listEqu.indexOf('+') > -1) {
int plus = listEqu.indexOf('+');
int prev = Integer.parseInt(listEqu.get(plus-1));
int nxt = Integer.parseInt(listEqu.get(plus+1));
Character sum = (char) (nxt + prev);
listEqu.set(plus, sum);
System.out.println(listEqu);
}
}
nxt and prev are char values. Tey take their value in the ASCII table, where '1' is 61 and 'b' is 142 (thus, '1' + '1' = 'b')
You need to substract '0' to get the number they represent. ('1' - '0' = 61 - 60 = 1)
The sum is not necessarily writable with one character, so you shouldn't put it back into a char array.
If you want to convert an integer to a string, use Integer.toString(i).
(And, if you want to, get the first character of the string and put it in the array, if that's what you want)
You need to parse the characters to their corresponding decimal value before you perform the addition, and then back to a character after. The methods Character.digit(char, int) and Character.forDigit(int, int) can do that (and I would use char since that is the type of prev and nxt). Like,
char prev = listEqu.get(plus - 1);
char nxt = listEqu.get(plus + 1);
Character sum = Character.forDigit(Character.digit(nxt, 10)
+ Character.digit(prev, 10), 10);

Java CASE why do i get a complete differet int back with and without using ' '

As a Java beginner I'm playing around with a case statement at this point.
I have set: int d = '1'; And with: System.out.println("number is: " + d);, this returns 51.
Now I found out that if I set it as: int d = 1;, it does return 1.
Now my question is why does it return 49 when I set it as '3'? What difference do the ' ' make?
My code that returns 49:
int a = '1';
switch(a)
{
case '1' :
System.out.println("Good");
break;
case '2' :
case '3' :
System.out.println("great");
break;
default :
System.out.println("invalid");
}
System.out.println("value: " + a);
'1' is the character '1', whose integer value is 49. Each character has a numeric value in the range from 0 to 2^16-1. Therefore 1 and '1' are different integer values.
With ' (single quotes) around a character you tell the compiler that the value in between is a character (variable type char in Java). Generally, computers only understand numbers so characters get represented as numbers in memory as well. The value of the character '1' is 49. You want to actually save the value 1 in memory and use that to calculate things. So, you have to tell the compiler that you want this to be interpreted as an actual number and not as a character.
To summarize:
'1' is the character that prints the number 1
1 is the actual number 1
This means 1 and '1' have different integer values.
You can look at the integer value of the most commonly used characters in coding here: ASCII Table
In Java the character values are stored as int internally. That means when you assign a char value using 'A' to an int type, the compiler casts the char value into int and thus the UTF-16 Code unit value is stored into the int variable.
Try this one as well:
int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
1 is interpreted as a character whose value is 49.
Do:
int a = 1;
switch(a) {
case 1:
...
Or, if you go with your current code:
System.out.println("value: " + Integer.parseInt((char)a+'');
It is happens because for some weird reason java treat char type as short.
Your '1' expression is constant expression of type char. It is stored internally as some numeric value. Usually all goes fine with this approach for example System.out.println('1') will print exactly what you expect.
But when you write int a = '1'; you char value is converted to int value because char behave like short int there.
PS: if there was no implicit conversion between char and int (which anyway have no sense) you just got compilation error.
When you declare int d='1'; then 1 is not treated as integer it is a character. It is converted to 49 according its unicode value. similarly character '3' will coverted to 51. Jvm do implicit type casting from character to integer.
you should try this code
char c = '3';
int a ='1';
System.out.println(c);
System.out.println((int)c);
You will get output as
3
51
49

Questions:Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure

solution:
public boolean isUniqueChars2(String str) {
if (str.length() > 256) return false;
boolean[] char_set = new boolean[256];
for (int i = 0; i< str.length(); i++) {
int val = str.charAt(i);
if (char_set[val]) {
return false;
}
char_set[val] = true;
}
return true;
}
I have some questions:
The codes is based on the premise that the given string is a ASCII string, what if it is a Unicode string? What's the difference between ASCII string and Unicode string?
int val=str.charAt(i)
I searched ASCII table,
If the string is "#ABCD", what number will return?(which of the three"Dec, Hx, Oct" will be use?)
The str.charAt(i) should return a character (e.g. A), why declaring the data type as int can directly convert the character to the accordingly ASCII table's number?
From the docs:
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).
To make your solution work for Unicode characters too, change these lines:
if (str.length() > 256) return false;
boolean[] char_set = new boolean[256];
To this instead:
if (str.length() > Character.MAX_VALUE + 1) return false;
boolean[] char_set = new boolean[Character.MAX_VALUE + 1];
int val=str.charAt(i) I searched ASCII table. If the string is "#ABCD", what number will return? (which of the three "Dec, Hx, Oct" will be use?)
You could just run the code and see for yourself.
"#ABCD".charAt(0) returns # of type char.
Converted to int that's 64 as per the ASCII table.
Dec, Hx, Oct are all just translations of the same thing.
Just like "book" in Spanish will be "libro",
63 in hexadecimal will be 0x3f.
The str.charAt(i) should return a character (e.g. A), why declaring the data type as int can directly convert the character to the accordingly ASCII table's number?
I guess this is how char is defined in the language itself,
but I can't find the authoritative reference.
The str.charAt(i) should return a character (e.g. A), why declaring the data type as int can directly convert the character to the accordingly ASCII table's number?
When you write int i = str.charAt(...), the Java language spec calls that Widening Primitive Conversion. You are allowed to do it because int and char are both integer-like numeric types, and every legal char value is also a legal int value.
http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html#25214
As for why ASCII? It's because Unicode was deliberately designed for compatibility with the US-ASCII character set. For every character in ASCII, it's Unicode encoding has the same numeric value as its ASCII encoding.
which of the three"Dec, Hx, Oct" will be use?
The numbers in the Dec, Hx, and Oct columns are just the same number represented with different place-value bases. Take the letter 'A' for example. When you express the numeric code for 'A' in decimal, it's "65". The same number expressed in base 16 is "41", and in base 8 it's "101". The table gives the codes in all three bases because all three are commonly used in computer programs, and in the documentation of computer hardware and software.
Base 8 and base 16 are commonly used because all modern computers represent numbers in base 2, and it's trivially easy to convert between base 2 and base 8 or base 16.
http://en.wikipedia.org/wiki/Hexadecimal
http://en.wikipedia.org/wiki/Octal

What does char casting in this Java println call do?

I'm new to programming in java and I do not understand a println statement. I can run it but cannot understand what it does. Can anyone explain what this does?
System.out.println((char) ('a' + 4));
'a' is a char literal;
char is an intergral numeric type in Java, just like int;
+ occurring between two numeric values means addition;
(char) indicates a conversion into a char type;
a char, when printed, renders as the Unicode codepoint it represents.
That simply prints out the letter e. #wrongAnswer has referenced the ASCII character codes, which is a numbering system that assigns unique IDs to characters. Take a look at this one for example: http://www.asciitable.com/.
If you take a look at the way the lower case letters are arranged in the table, you'll see that the letter a is at 97, b is at 98, c is at 99 and so on. What the above code is doing is that we are finding what the ASCII code of a is, then adding by 4, then casting back to char. Therefore, what this is actually doing is taking 97 then adding with 4, producing 101. If you consult the table, 101 is actually e, and so the output is e after casting 101 with char.
the ascii value of a is 97 so 97+4=101 101 which is ascii value of alphabets e.
System.out.println((char) ('a' + 4));
so the above sop will print e.
Lets break this down into discrete statements:
char a = 'a'; // the character a
int aPlusFour = a + 4; //characters are really an integer value for the ASCII code, so we can add four
char aPlusFourChar = ((char) aPlusfour); //cast our integer back to a char
System.out.println(aPlusFourChar); //this should print the character e
'a' == 97
97+4 == 101
(char)101 == 'e'
System.out.println('e'+""); // prints 'e'
First a ascii value is added to the value 4. and the character respective to the value 101 is printed..it is 'e'.You can refer ascii table

Categories