This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Character array of primitive elements initialization

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

key[0]=128;
key[1]=32765;
key[2]='Z';
key[3]=65535;

It is giving the output ? ? Z ?. I thought,byte and short types are implicitly conver to char.It converted.But Why,It is giving ? for everything.Is char arrays,using ASCII codes for numbers from 0 to 127.What about the numbers from 128 to 65535.Please share your knowledge.

Thanks,
Shiva
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For character values outside of the ascii range, you will likely get jibberish, unless you specify a locale
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it wasn't all that helpful. Here is a link that might be more useful then my vague blathering http://java.sun.com/j2se/1.4.2/docs/guide/intl/fontprop.html.

There are some good tutorials at java.sun.com, a search should help you out if you need to display other types of characters.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The characters outside 0-127 range are converted to ? by PrintStream's print method.

You can either use a Writer to convert the characters to correct encoding or DataOutputStream to write the 2-byte value or char.

If you want to print the value as a number (with PrintStream), than cast it to int.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic