Java 十進(jìn)制轉(zhuǎn)二、八、十六進(jìn)制的字符串
十進(jìn)制轉(zhuǎn)二進(jìn)制
class DecToBin
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
long dec = -9223372036854775807l;
// -9223372036854775808 這個(gè)數(shù)不行,不要試,嘿嘿
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);
}
}
十進(jìn)制轉(zhuǎn)八進(jìn)制
class DecToOct
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
long dec=-0;//有-0 嗎?
String octStr="";
long decAbs=Math.abs(dec);
while (decAbs>0)
{ octStr=(decAbs&7)+octStr;//
decAbs>>=3;
}
octStr= dec<0?"-"+octStr:dec==0?"0":octStr;
System.out.println(octStr);
}
}
十進(jìn)制轉(zhuǎn)十六進(jìn)制
class DecToHex
{
public static void main(String[] args)
{
System.out.println("Hello World!");
long dec=-1;//計(jì)算器的負(fù)數(shù)不會(huì)弄 -。-
String hexStr="";
long decAbs=Math.abs(dec);
while(decAbs>0)
{ long lastFour=decAbs&15;
if (lastFour>9)
hexStr=(char)('A'+lastFour-10)+hexStr;
else hexStr=lastFour+hexStr;
decAbs>>=4;
}
hexStr= dec<0?"-"+hexStr:dec==0?"0":hexStr;
System.out.println(hexStr);
}
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
hashMap擴(kuò)容時(shí)應(yīng)該注意這些死循環(huán)問(wèn)題
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著hashMap擴(kuò)容時(shí)的死循環(huán)問(wèn)題展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Mybatis-Plus開發(fā)提速器generator的使用
本文就介紹這款基于Mybatis-Plus的代碼自助生成器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序,結(jié)合具體實(shí)例形式分析了java樹形選擇排序的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05
Java實(shí)現(xiàn)簡(jiǎn)易HashMap功能詳解
這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)易HashMap功能,結(jié)合實(shí)例形式詳細(xì)分析了Java實(shí)現(xiàn)HashMap功能相關(guān)原理、操作步驟與注意事項(xiàng),需要的朋友可以參考下2020-05-05
JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用
這篇文章主要介紹了JAVA 靜態(tài)代理模式詳解及實(shí)例應(yīng)用的相關(guān)資料,這里舉例說(shuō)明java 靜態(tài)代理模式該如何使用,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-11-11
Java實(shí)現(xiàn)讀取Excel文件功能(EasyExcel初使用)
EasyExcel是一款基于Java語(yǔ)言的開源Excel解析工具,可以幫助我們快速、高效地讀取和寫入Excel文件,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)讀取Excel文件功能的相關(guān)資料,使用的是EasyExcel,需要的朋友可以參考下2024-07-07

