Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java long to binary

Tags:

java

something I thought was simple ends up not so much.

I need to convert a long number to binary.

For example:

String b =  Integer.toBinaryString(1028);

the output is 10000000100

but when I use Integer.toBinaryString(2199023255552); it does not work. Of course the number is too big for this function and I can't find one that does convert from long.

Any suggestions?

Thank you.

like image 732
Steve Avatar asked Aug 07 '11 01:08

Steve


1 Answers

Add an L to indicate its a long<1> and use the Long class<2>:

Long.toBinaryString(2199023255552L);

<1> Constants in java are considered ints unless you specify otherwise.

<2> Integer.toBinaryString() receives an int as parameter, not long.

like image 134
MByD Avatar answered Sep 30 '22 17:09

MByD