Java 十进制转二、八、十六进制的字符串
十进制转二进制
classDecToBin
{
publicstaticvoidmain(String[]args)
{
//System.out.println("HelloWorld!");
longdec=-9223372036854775807l;
//-9223372036854775808这个数不行,不要试,嘿嘿
StringbinStr="";
longdecAbs=Math.abs(dec);
while(decAbs>0)
{binStr=(decAbs&1)+binStr;
decAbs>>=1;
}
binStr=dec<0?"-"+binStr:dec==0?"0":binStr;
System.out.println(binStr);
}
}
十进制转八进制
classDecToOct
{
publicstaticvoidmain(String[]args)
{
//System.out.println("HelloWorld!");
longdec=-0;//有-0吗?
StringoctStr="";
longdecAbs=Math.abs(dec);
while(decAbs>0)
{octStr=(decAbs&7)+octStr;//
decAbs>>=3;
}
octStr=dec<0?"-"+octStr:dec==0?"0":octStr;
System.out.println(octStr);
}
}
十进制转十六进制
classDecToHex
{
publicstaticvoidmain(String[]args)
{
System.out.println("HelloWorld!");
longdec=-1;//计算器的负数不会弄-。-
StringhexStr="";
longdecAbs=Math.abs(dec);
while(decAbs>0)
{longlastFour=decAbs&15;
if(lastFour>9)
hexStr=(char)('A'+lastFour-10)+hexStr;
elsehexStr=lastFour+hexStr;
decAbs>>=4;
}
hexStr=dec<0?"-"+hexStr:dec==0?"0":hexStr;
System.out.println(hexStr);
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!