Java decimal to binary, octal, hexadecimal string
•
Java
Decimal to binary
class DecToBin
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
long dec = -9223372036854775807l;
// -9223372036854775808 这个数不行,不要试,嘿嘿
String binStr="";
long decAbs=Math.abs(dec);
while (decAbs>0)
{ binStr=(decAbs&1)+binStr;
decAbs>>=1;
}
binStr= dec<0?"-"+binStr:dec==0?"0":binStr;
System.out.println(binStr);
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
