Use sprintf("%08b", num) to convert input to binary number : sprintf « Development « Ruby






Use sprintf("%08b", num) to convert input to binary number


#!/usr/bin/env ruby

module Binary

  def Binary.to_bin( num )
    bin = sprintf("%08b", num)
  end

end

p Binary.to_bin( 123 ) # => "01111011"

 








Related examples in the same category

1.Field types for sprintf
2.Sprintf Flags and Field Types
3.Print out time in Begin and end block
4.sprintf( "%0.2f", my_bmi )