如何用0填充二进制字符串?

时间:2022-05-26 21:27:42
string binary = Convert.ToString(15, 2);

Console.WriteLine("{0}", binary);

Prints:

打印:

1111

1111年

I want it to print 00001000

我要打印00001000

Because the data type is of string and not integer I cannot do something like this:

因为数据类型是字符串而不是整数,所以我不能这样做:

Console.WriteLine("{0:00000000}", binary);

2 个解决方案

#1


20  

Console.WriteLine( binary.PadLeft(8, '0'));

#2


8  

You can try this one:

你可以试试这个:

Convert.ToString(15, 2).PadLeft(8, '0');

转换。2)ToString(15日.PadLeft(8 ' 0 ');

It should give you 00001111

应该是00001111

#1


20  

Console.WriteLine( binary.PadLeft(8, '0'));

#2


8  

You can try this one:

你可以试试这个:

Convert.ToString(15, 2).PadLeft(8, '0');

转换。2)ToString(15日.PadLeft(8 ' 0 ');

It should give you 00001111

应该是00001111