How to pad a binary string with zeros?

℡╲_俬逩灬. 提交于 2019-12-18 05:50:16

问题


string binary = Convert.ToString(15, 2);

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

Prints:

1111

I want it to print 00001000

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

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

回答1:


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



回答2:


You can try this one:

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

It should give you 00001111



来源:https://stackoverflow.com/questions/7682628/how-to-pad-a-binary-string-with-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!