转换成零填充二进制字符串整数 [英] Convert integer to zero-padded binary string

查看:172
本文介绍了转换成零填充二进制字符串整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

INT转换时,二进制,如何将其输出到8个字符的,目前只显示1和短的7零

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')

将产生 00000001

这篇关于转换成零填充二进制字符串整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆