本文介紹了將整數(shù)轉(zhuǎn)換為零填充的二進(jìn)制字符串的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
int轉(zhuǎn)二進(jìn)制時(shí),如何輸出為8個(gè)字符,目前只顯示1,少了7個(gè)零
when converting int to binary, how to output it to 8 character, currently it only display 1 and short of the 7 zeros
int x = 1;
String bin = Integer.toBinaryString(x);
System.Out.Println(bin);
示例輸出到 0000 0001
example output to 0000 0001
推薦答案
我不確定這是否是你的意思,但是像這樣的東西怎么樣
I am not sure if that is what you mean but how about something like
String.format("%8s", Integer.toBinaryString(1)).replace(' ', '0')
會(huì)生成00000001
這篇關(guān)于將整數(shù)轉(zhuǎn)換為零填充的二進(jìn)制字符串的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!