Ale*_*pov 5 ruby string binary integer
我知道我可以使用Fixnum#to_s二进制格式将整数表示为字符串.然而1.to_s(2)产生1,我希望它生产00000001.如何使所有返回的字符串都填充为8个字符的零?我可以使用类似的东西:
binary = "#{'0' * (8 - (1.to_s(2)).size)}#{1.to_s(2)}" if (1.to_s(2)).size < 8
Run Code Online (Sandbox Code Playgroud)
但这似乎并不优雅.