新手问题 显示转换二进制字符串怎么用前导 0 补齐

yakczh · August 24, 2015 · Last by luikore replied at August 24, 2015 · 1969 hits
"abcd123".each_byte do |byte |

  printf(" %-08s",byte.to_s(2))

end 

想输出

0110 0001     0110 0010      0110  0011       0110 0100     0011 0001      0011 0010     0011 0011  
"abcd123".each_byte do |byte |
  printf(byte.to_s(2).rjust(8,'0')+" ")
end

"abcd123".each_byte do |byte |

  printf(" %08b", byte)

end 
"abcd123".unpack('B*').first.scan /(.{4})(.{4})/ do |a,b|
  print "#{a} #{b}     "
end
You need to Sign in before reply, if you don't have an account, please Sign up first.