如何输出指定位数的二进制或十六进制数

lianglin999 2010-10-26 10:37:13
RT:
例如:

int a = 100;
System.out.println (Integer.toBinaryString (a));


输出是:1100100

而我想的是输出8位的二进制数:01100100

请教该如何实现
...全文
730 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kyo19 2010-10-28
  • 打赏
  • 举报
回复
DecimalFormat df = new DecimalFormat("00000000");

就是指定它的输出格式,如果有数的会显示原来的数字,没有的显示0,你输入的是7位,因为最开始的0被省略了

下面的Integer.valueOf(bin) 是因为通过format出来的是个String类型,但你查看API时df.format(...)中传的没有String这个类型,所以我只是把他转为Int类型而已
thegodofwar 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kyo19 的回复:]
Java code
public static void main(String[] args) {
int a = 100;
String bin = Integer.toBinaryString(a);
System.out.println (Integer.toBinaryString (a));
De……
[/Quote]
+1不过为了扩大范围,可以把
String format = df.format(Integer.valueOf(bin));
改为
String format = df.format(Long.valueOf(bin));
Miracle1216 2010-10-26
  • 打赏
  • 举报
回复

public class Test {
public static void main(String[] args) {
int a = 100;
System.out.println(Integer.toBinaryString(a));
System.out.println(convert(Integer.toBinaryString(a), 8));
}

private static String convert(String str, int length) {
if (str.length() < length) {
String temp = "";
for (int i = 0; i < length - str.length(); i++) {
temp += "0";
}
return temp + str;
}
return str;
}
}
kyo19 2010-10-26
  • 打赏
  • 举报
回复
	public static void main(String[] args) {
int a = 100;
String bin = Integer.toBinaryString(a);
System.out.println (Integer.toBinaryString (a));
DecimalFormat df = new DecimalFormat("00000000");
String format = df.format(Integer.valueOf(bin));
System.out.println(format);
}
precious 2010-10-26
  • 打赏
  • 举报
回复
static String getBinaryFromInt(int b) {
StringBuffer sb = new StringBuffer();
for (int i = 7; i >= 0; i--) {
sb.append(((b & (1 << i)) != 0) ? '1' : '0');
}
return sb.toString();
}

输出是:01100100
liudan3319 2010-10-26
  • 打赏
  • 举报
回复
DecimalFormat df=new DecimalFormat("00000000");
System.out.println("ffffffffffffffffffff"+df.format(111111));
result:ffffffffffffffffffff00111111
龙四 2010-10-26
  • 打赏
  • 举报
回复
System.out.printf自己设置格式
liudan3319 2010-10-26
  • 打赏
  • 举报
回复
DecimalFormat
closewbq 2010-10-26
  • 打赏
  • 举报
回复
判断长度,自己补零
lianglin999 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kyo19 的回复:]

Java code
public static void main(String[] args) {
int a = 100;
String bin = Integer.toBinaryString(a);
System.out.println (Integer.toBinaryString (a));
System.out.println (Integer.toBinaryString (a));
DecimalFormat df = new DecimalFormat("00000000"); //不知道这个是什么意思,能解释一下吗,看帮助文档也无法理解,谢谢!
String format = df.format(Integer.valueOf(bin)); //还有这一句?
System.out.println(format);
[/Quote]
刚刚学习Java,请指教
zrcvic 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ticmy 的回复:]

System.out.printf自己设置格式
[/Quote]+1

用 DecimalFormat 简直浪费!
ETCentury 2010-10-26
  • 打赏
  • 举报
回复
6楼,7楼都不错~!
ESC:显示或隐含扩展输入框 F1: 显现帮助窗口 ESC:关闭帮助窗口 C: 清输入框和输出值 AU:所见即得计算值 (hd:关闭所见即得功能) MC:保留 M+: 累加累减寄存器 (鼠标右击转换累加或累减) Mn:寄存器,可以存储十组据 (在Mn状态下,空格清空输入框, 拖曳据,自动保存->M0~M9) (在M0~M9状态下,空格清空输入 框,回车,自动清除->Mn) ≈≈扩展部分≈≈ 复原: 表达式宽(685)、显示宽度(14)、 小(6)复原 以及所有据初始化 表达式宽: 表达式宽度:扩展输入表达式的宽度 显示宽度:输入输出字精度 位指定输出 (超出部分截断) 调色板:$000000~$ffffff,BGR (':'左右分别是底色和文字本色) 进制转换: toHex:输入十进制,输出十六进制 toBin:输入十进制,输出二进制 toDec:$输入十六进制输出十进制 toDec:b输入二进制输出十进制 学公式: mod:AmodB(得余。输入A、B为十进 制,输出十进制) div:AdivB(A整除B,得整。输入A、 B为十进制,输出十进制) X^:A的B次方(2X^3=8) 2X^:2的B次方(2X^3=8) logN:X^的逆函(8logN2=3) logN2:2X^的逆函(8logN2=3) 逻辑运算: (and):求与 (or):求或 (not):求非 (Xor):求异或(相异为1,相同为0) 转换函: sin()、cos()、tg(): exp():eX^A(e=2.7182818284 eX^1) ln():exp()的逆函 Char():输入字符,输出ASCII对应码 (Char(a)=97) Byte():输入ASCII对应码,输出字符 (Byte(30)='0') 使用技巧: 各输入框支持鼠标拖曳功能,用鼠标 划过选定复制内容,进行拖曳。放落后 落点在光标所在处 delphi 源码开放

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧