• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i give binary value to int?  RSS feed

 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!