将整数转换为带有前导零的二进制字符串 [英] Convert an integer to a binary string with leading zeros
本文介绍了将整数转换为带有前导零的二进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将int转换为bin并添加额外的位.
I need to convert int to bin and with extra bits.
string aaa = Convert.ToString(3, 2);
它返回11
,但是我需要0011
或00000011
.
it returns 11
, but I need 0011
, or 00000011
.
如何完成?
推荐答案
11
是二进制表示形式 3
.此值的二进制表示形式是2
位.
11
is binary representation of 3
. The binary representation of this value is 2
bits.
3 = 2 0 * 1 + 2 1 * 1
3 = 20 * 1 + 21 * 1
您可以使用 String.PadLeft(Int, Char)
方法添加这些零.
You can use String.PadLeft(Int, Char)
method to add these zeros.
Convert.ToString(3, 2).PadLeft(4, '0') // 0011
Convert.ToString(3, 2).PadLeft(8, '0') // 00000011
这篇关于将整数转换为带有前导零的二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文