• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can i give binary value to int?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I want to know how to give binary value to int. Like how we give for hexa decimal(starting with 0X) and for octal(starting with 0). Is there any convention that has to be followed to give binary value to int.

Thanks,
Ravindranath.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can not do this directly. The java language supports "only" octal, hexadecimal and decimal numbers directly.

To assign a binary value to a number you need to convert it first, ie:


For conversion check out Integer.parseInt(String s, int radix).

Why do you want to do this ?

pascal
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I can't able to get the int from the binary with the

convertToBinary("10001000001001");
Can you please clear my doubt.

Thanks,
Ravindranath.
 
author
Posts: 23951
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

Originally posted by Ravindranath Chowdary:
Hi,
I can't able to get the int from the binary with the

convertToBinary("10001000001001");
Can you please clear my doubt.

Thanks,
Ravindranath.



First, If you read the previous post again, you'll notice that it is Pseudo Code, and that you should look at Integer.parseInt() for more information.

Second, please don't do this... To create a new string object, which is then parsed, just because you want to be able to read binary in the source code is silly. Most developers just get comfortable with reading hexidecimal. (Not to mention that you will probably be challenged at your next code review for doing this)

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic