• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unicodes in Java: how to use them?  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find that unicodes tablet is outdated, or I do something wrong.

http://www.ssec.wisc.edu/~tomw/java/unicode.html
I wanted to print 'dollar symbol' to console.



Console output: x0024

---------------------------------------------------------
Another site.
http://unicode-table.com/en/#control-character

This is easier to watch. I dont understand how to use this unicode ( what part ) ?
unicodded.png
[Thumbnail for unicodded.png]
unid.png
[Thumbnail for unid.png]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are simply using the wrong syntax. It should be:

Note that the console uses a font that does not contain all Unicode characters. For many characters, you'll probably get "?" because the font doesn't contain that character.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are actually doing is using an octal escape \0 for the null character and then printing the remainder of the String. As Jesper says, \u0024.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bob,
I know what are you confused in, because some days back I was also in confusion to all these.
Let me explain it to you. what you are trying to do is instead of directly typing the $ sign you want it to print using hexadecimal in between the String.
Now let us come to java, for Strings, what java provide is a escape character '\' backslash for the purposes where you want to add some additional characters which are not under your keyboard, and also for many other purposes. Now how to use that escape character and with whom(octal, hex, decimal) we have Java language specificatiion which explains it very clearly :
Octal escapes are provided for compatibility with C, but can express only Unicode values \u0000 through \u00FF, so Unicode escapes are usually preferred.

Now what you are doing wrong here is, you are using the escape character with the wrong type of value. In "\0x0024" you are escaping just a octal 00 (not hex, as said above) and then printing the rest as a normal string.
Means, escape character in string accepts either a octal value or a unicode value(other the escape sequences).
So the correct way will be :
or


But yes you can also use hex numbers for printing $ , but not inside a string or using a escape character, instead directly printing them

what you will notice here that it will print 36, because it is the decimal value of 0x0024. If you want to print character using hex value, then cast it in char.


Hope I am able to clear your doubts.
Thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:
what you will notice here that it will print 36, because it is the decimal value of 0x0024. If you want to print character using hex value, then cast it in char.


Even though it works, it's still kludgy. If you want to print a char, then use a char literal:

If you want a char variable, then declare a char variable and give it a char value:

Another way is to use the %c format specifier:

The point is, use literals that are most appropriate for the type you are using. You only make your code convoluted by relying on automatic type conversions or forcing conversions with casting.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:. . . with whom(octal, hex, decimal)
There are no escapes for decimal literals. There are literals for binary numbers, but not escapes; they were only available in Java7. I think binary is so basic that binary literals shou‍ld have been available from the very beginning.
. . .  If you want to print character using hex value, then cast it in char.
. . .
As you have been told, that is not ideal programming. The opposite can also be done:-
System.out.println(+c);
The unary + operator causes the char c to be promoted to 32 bits (i.e. an int) and that code will print 36 for your '$' char.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu and Campbell for correcting me and giving me more insight into this.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem. Glad we could help.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!