java输出二进制高位补0
/**整数转二进制,前面补零*/
public static String intToBinary(int num){
//1.补零
String binaryStr = Integer.toBinaryString(num);
int bitNum = 8;
if(bitNum<binaryStr.length()) {
bitNum += bitNum;//不断翻倍8 16 32 64...
}
while(binaryStr.length() < bitNum){
binaryStr = "0"+binaryStr;
}
//2.格式化
String str = "";
for (int i = 0; i < binaryStr.length();) {
str += binaryStr.substring(i, i=i+4)+",";
}
return str.substring(0, str.length()-1);
}
测试
System.out.println(Integer.toBinaryString(996));
System.out.println(intToBinary(996));
输出
1111100100
0000,0011,1110,0100
资料
https://blog.csdn.net/libertine1993/article/details/100876919
https://blog.csdn.net/lonely_fireworks/article/details/7962171