• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

converting binary to decimal

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

i tried this command

System.out.println(Byte.parseByte("10000001",2));

while running it it throws

Exception in thread "main" java.lang.NumberFormatException: Value out of range
Value:"10000001" Radix:2
at java.lang.Byte.parseByte(Unknown Source)
at Test.main(Test.java:19)


AS byte can range from -127 to +127 and why its not allowing this value which i think -127

IF i'm wrong please correct me..........
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AS byte can range from -127 to +127 and why its not allowing this value which i think -127

IF i'm wrong please correct me..........



The parse routine doesn't treat radix 2 different from any other radix. The value of "-127" is "-1111111" with radix 2. It doesn't understand the concept of a sign bit. Now I agree that this is awkward for binary calculations.

Henry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to Henry's post:
Notice that to get -128 you would have to parse the string "-10000000". Do not mistake the computer's internal twos-compliment notation with the standard mathematical binary number system notation.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic