• Post Reply Bookmark Topic Watch Topic
  • New Topic

binary to long  RSS feed

 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i convert an long value into binary ?? and i have to store it as long ...

(is there any way to store as binary !!! )

binVal = Integer.parseInt(Integer.toBinaryString(25));//for int it is working
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A long is 64bit, while an int is 32bit. So, you can represent a long as two ints:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manisekar Chinnasami:
how can i convert an long value into binary ?? and i have to store it as long ...

(is there any way to store as binary !!! )

binVal = Integer.parseInt(Integer.toBinaryString(25));//for int it is working

Whether a number is "binary" or not is not a special property of a number. Do you understand what your line of code does?

binVal = Integer.parseInt(Integer.toBinaryString(25));

First, you convert the number 25 to a string of binary digits: "11001"
Then, you interpret this as if this is a decimal number (with the value eleven thousand and one).

This does not "store a number as binary". There is no such thing as "storing a number as binary", or rather, all numbers in the memory of a digital computer are stored as binary!

Anyway, class Long also has a method toBinaryString() and a method parseLong().

Note: If you have a string of binary digits which you want to convert to an Integer or to a Long, use the parseInt(...) or parseLong(...) method with two parameters, and specify 2 for the radix:

[ November 30, 2007: Message edited by: Jesper Young ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!