• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Mysterious reading

 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to understand behavior of System.in.read(byte[]) .

In the below code.



CONSOLE:
Ð
1
-48---11111111111111111111111111010000
Ð




Ð----is a character with Unicode value of U+0189 and its 2 byte or 16 bit representation is 0000 0001 1000 1001
Now , output of line2 says that , System.read has read 1 byte from console (std input) ,
Question 1 is which byte of Ð does it read , 0x01 or 0x89 ???
Question 2 is why is it printing -48 in line no 3.
Also i couldn't understand ,HOW String constructed 2 byte information Ð, even though it only read one byte?



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Don Redd wrote:Ð----is a character with Unicode value of U+0189 and its 2 byte or 16 bit representation is 0000 0001 1000 1001
Now , output of line2 says that , System.read has read 1 byte from console (std input) ,
Question 1 is which byte of Ð does it read , 0x01 or 0x89 ???
Question 2 is why is it printing -48 in line no 3.
Also i couldn't understand ,HOW String constructed 2 byte information Ð, even though it only read one byte?


Ooof. Where to start?

First, you have several misconceptions going on:
1. You are NOT reading in Unicode, or even characters, you are reading in bytes.
2. It's highly unlikely that your system console uses any multibyte character set at all. In fact, I'd care to bet that it uses Windows-1252.
3. Ð in Windows-1252 is code 208 (decimal), NOT 0189.
4. Java bytes are signed.
5. What do you get when you try to print out the value of Byte.valueOf((byte)208)?

HIH

Winston
 
Water proof donuts! Eat them while reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic