• Post Reply Bookmark Topic Watch Topic
  • New Topic

Int to Char  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

during my studying I come to face with a lot of questions that expect me to know what is the correlation between ints and chars.
For example :

I learned that char  y= 'a'  is 97 in int and 'b' is 98 in int (int i = (char)y)

My question is the following:
In what way are ints connected to char and the opposite?
Can you recommend me some websites that explain this correlation and I can read in order to learn how to transform an int to char and the opposite??
Thanks
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorials have a section on Primitive Data Types.
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Un char is an int unsigned.

 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to look in the Java® Language Specification as well. Beware: that can be difficult to read. Note as DU said, chars are unsigned, so unlike ints they don't have negative values. If you cast an int to a char you get an unsigned representation of the rightmost 16 bits, b₀...b₁₅.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:The Java Tutorials have a section on Primitive Data Types.


In this section is does not mention how to convert an int into char and the opposite
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure there examples somewhere but the conversions are quite straightforward.

Since char is an integer type and char is smaller than int, a conversion from char to int happens transparently/automatically when one operand of an expression is an int. This is called a widening conversion and you can search the JLS for more about those.

The expression 'a' + 1 has a char operand and an int operand. Therefore, the char operand will be widened to an int before 1 is added to it. The int value of a char is its Unicode value.

Going from int to char is a narrowing conversion and requires a cast, as you have already seem to have learned. Since a cast is always explicit, it is the programmer's responsibility to ensure that the int being cast to a char actually is a valid Unicode value since the range of valid Unicode values is smaller than the range of int values.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: The int value of a char is its Unicode value.



OK that was what I was asking..
How for example 'a' was 97..
But I found it in the table of Unicode characters !!!

Latin
Alphabet:
Lowercase U+0061 a a Latin Small Letter A

thanks !!
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you follow the search link I provided above, you'll find a number of Unicode tables. This page shows the Unicode values from \u0000 to \u001F: https://unicode-table.com/en/

The char 'a' has a Unicode value of \u0061 which you can get by adding its row label, 0060, and its column label, 1. (See the numbers on the left and top of the table, respectively). These are hexadecimal values so 0x0061 is 97 in decimal (16 * 6 + 1 * 1). Similarly, 'A' has a Unicode value of \u0041 which is 65 decimal.

If you're not familiar with hexadecimal notation, just look it up.

Note that the integer values for the letters 'a' to 'z', 'A' to 'Z', and the digits '0' to '9' are consecutive. This is why the expression (char)('a' + 3) evaluates to the char value of 'd'. This is also the reason you can use integer expressions to calculate an index using char values. So if you were to use an array to map letters to something, it's fairly straightforward:



EDIT: the danger of composing posts on a mobile device using only your thumbs to type... OP answers before you can post a follow-up!
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a little bit of trivia: The Java compiler will recognize and treat Unicode escape sequences as literals. This line of code below is valid Java, although I certainly wouldn't recommend doing it except as a parlor trick. Try it and see what it does.

And this is the code that helped me produce that (I've elided the String literal on line 1 so as not to give away the answer for the above code):
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many questions about conversion between char ,int, short, byte if you will take OCA exam.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to the information above I am trying to build a small program that will produce 10 different random words that are build from random characters from 'a' to 'z' . words length is randomly from 1 to 6 characters.
but I received strange output. words were more than 6 characters long and they repeated frequently in each same word. please I need help to understand what is wrong.





run:
eeo 
llbllbs 
ccuccuxccuccuxi 
eereeraeereeraneereeraeereeranoeereeraeereeraneereeraeereeranob 
yyfyyfnyyfyyfnqyyfyyfnyyfyyfnqtyyfyyfnyyfyyfnqyyfyyfnyyfyyfnqtk 
rrj 
jjhjjho 
nnjnnjgnnjnnjgy 
ttsttsfttsttsfdttsttsfttsttsfduttsttsfttsttsfdttsttsfttsttsfduu 
ddxddxdddxddxdyddxddxdddxddxdyn 
BUILD SUCCESSFUL (total time: 0 seconds)
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 16 is what's making the return value longer than what you expected.  If you're going to use += operator, you don't need the word variable on the right side since it's implicitly added already by the += operator.  The expression you have now is equivalent to

NOTE: it's poor form to hard code values like 25 and 97 like that.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I would recommend you use a Random object rather than doing arithmetic with Math#random. Search my posts for Random and nextInt and you shou‍ld find something useful.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you Junilu and Campbell,  I'm making a tour right now in Jerusalem, I will continue with Java as soon as I return home
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!