ASCII value related query - java

I read that "When an integer is cast into a char, only its lower 16 bits of data are used; the other part is ignored". Based on this shouldn't i get the char value for '0041' as output.Instead i get 'A' as output ,which has an ASCII value of 65. Why does this happen??
public class practice {
public static void main(String[] args) {
char ch = (char)0XAB0041;
System.out.println(ch);
char ch1= (char)65.25;
System.out.println(ch1);
}
}
Will i get the same output if i myself consider only the lower 16 bits for casting.As below:
char ch = (char)0041;
System.out.println(ch);
Guys could anyone clear this problem that i am facing in comprehending the relation between unicode,ASCII and hexadecimal values...
Thanks..

0x0041 is decimal 65 which is ASCII 'A'.
65.25 would be truncated to 65, so it's still 'A'.
What were you expecting?

0XAB0041 in decimal is:
11206721
in binary, it becomes:
101010110000000001000001
So taking last 16 bits,
we have: 0000000001000001=65 in decimal
If you see the ASCII table in this link, it is for 'A'
Hence, 0XAB0041 on casting to char, becomes 'A'
If you consider
char ch = (char)0041;
System.out.println(ch);
0041 is taken by java as octal literal with decimal value 4*8+1=33.So ASCII code for 33 decimal is !.
Hence the output will be:
!
Hence if you ask "Will i get the same output if i myself consider only the lower 16 bits for casting.", your answer is !(not) :)

You are dealing with 0x41 which is 65(16 * 4 + 1) in decimal system. 'A' corresponds to ascii 0x41.

Related

Char to int and back again

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'

Long to hex String number of characters

Hi i am trying to build a random 16 characters hex, to do so i tried a Long.toHexString(new Random().nextLong() my assumption is that it will always return a 16 chars string, Am i right ? (Once it returned 15 chars)
Take a look at the javadocs for toHexString(long i) (emphasis mine).
public static String toHexString(long i)
Returns a string
representation of the long argument as an unsigned integer in base 16.
The unsigned long value is the argument plus 264 if the argument is
negative; otherwise, it is equal to the argument. This value is
converted to a string of ASCII digits in hexadecimal (base 16) with no
extra leading 0s. If the unsigned magnitude is zero, it is represented
by a single zero character '0' ('\u0030'); otherwise, the first
character of the representation of the unsigned magnitude will not be
the zero character.
As it turns out, it will not always be 16 characters long. However you can pad with zeros if you want like so:
import java.util.Random;
class Main {
public static void main(String[] args) {
String hex16Chars = String.format("%016X", new Random().nextLong());
System.out.println(hex16Chars + ", len: " + hex16Chars.length());
}
}
You will see the length is always 16 as expected.
And it also turns out peeking at the docs actually helps! :)
Referring to Javadoc of the method in question should be your first port of call:
This value is converted to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0s
So no, it won't always be 16 chars.
However, you can print a 16-char uppercased hex string, with leading zeros, using:
String.format("%016X", longValue)

adding characters using charAt in java is giving me the wrong sum

int x=(int)compressedText.charAt(one1+1);
int y=(int)compressedText.charAt(one1+2);
count=x+y;
count1=(char)count;
the craracter value for compressedText.charAt(one1+2) and compressedText.charAt(one1+1) are each equal to 1 but when I try to debug my code it says count is equal to 98.
Casting a char that represents a numeric character to an int doesn't do what you think it does. It takes the Unicode value of the char (which is 49 for '1'). That explains why you get 98 instead of 2.
Because the code values for the characters '0' through '9' are 48 through 57, you can subtract '0' (48) from each char instead, e.g.
int x = compressedText.charAt(one1+1) - '0';
You'll need to undo this conversion if you are converting an int back to a char that is meant to represent the numeric character. Also you'll need to account for multiple characters if count is more than one digit (>= 10).

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

How does subtracting the character '0' from a char change it into an int?

This method works in C, C++ and Java. I would like to know the science behind it.
The value of a char can be 0-255, where the different characters are mapped to one of these values. The numeric digits are also stored in order '0' through '9', but they're also not typically stored as the first ten char values. That is, the character '0' doesn't have an ASCII value of 0. The char value of 0 is almost always the \0 null character.
Without knowing anything else about ASCII, it's pretty straightforward how subtracting a '0' character from any other numeric character will result in the char value of the original character.
So, it's simple math:
'0' - '0' = 0 // Char value of character 0 minus char value of character 0
// In ASCII, that is equivalent to this:
48 - 48 = 0 // '0' has a value of 48 on ASCII chart
So, similarly, I can do integer math with any of the char numberics...
(('3' - '0') + ('5' - '0') - ('2' - '0')) + '0') = '6'
The difference between 3, 5, or 2 and 0 on the ASCII chart is exactly equal to the face value we typically think of when we see that numeric digit. Subtracting the char '0' from each, adding them together, and then adding a '0' back at the end will give us the char value that represent the char that would be the result of doing that simple math.
The code snippet above emulates 3 + 5 - 2, but in ASCII, it's actually doing this:
((51 - 48) + (53 - 48) - (50 - 48)) + 48) = 54
Because on the ASCII chart:
0 = 48
2 = 50
3 = 51
5 = 53
6 = 54
In C the + and - operators apply integer promotion*1 to their arguments, thus the result of subtracting (or adding) two chars is an int*2.
From the C-Standard:
5.1.2.3 Program execution
[...]
10 EXAMPLE 2 In executing the fragment
char c1, c2;
/* ... */
c1 = c1 + c2;
the ‘‘integer promotions’’ require that the abstract machine promote the value of each variable to int size and then add the two ints [...]
Applying this to the OP's implicitly given use case of
char c = 42;
... = c - `0`;
This would be lead to the above being the same as:
... = (int) c - (int) `0`; /* The second cast is redundant, as per Jens' comment. */
^ ^
+------ int -----+
*1: If the operators arguments have a lower rank than int they are promoted to an int.
*2: char has a lower rank than int.
There's no change going on. '0' is an int in C. It is a fancy way to write 48 (assuming ASCII).
You can convince yourself of this fact by computing the size of '0':
printf ("sizeof '0' is %zu\n", sizeof '0');
printf ("sizeof(char) is %zu\n", sizeof(char));
which in the first line very likely prints 4 (or 2) but probably not 1 like in the second row (again: in C; it's different for C++).
The numeric constant 0, without any qualifications, has type int. The result of the binary subtraction operation on a char and an int also has type int due to the usual type promotion process.

Categories