Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# binary to long

Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
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

Manisekar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19413

13

A long is 64bit, while an int is 32bit. So, you can represent a long as two ints:

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 13540

6

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala

It is sorta covered in the JavaRanch Style Guide.

subject: binary to long