programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# binary to long

Manisekar Chinnasami
Greenhorn
Posts: 24
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
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
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 ]