Sha256: dca08d5b86f3a38ceed8d43862caf2b4db4cf0fca0eab7cd1a22984c00bd48db

Contents?: true

Size: 838 Bytes

Versions: 69

Compression:

Stored size: 838 Bytes

Contents

# encoding: utf-8

# Numeric
class Numeric
  # return is digit table
  #
  # ==== Examples
  #
  # 255 to 256 case
  #
  #   Numeric.to_digit_table(255, 256)
  #
  # result
  #
  #   |10digit|          2digit|8digit|16digit|
  #   |    255|0000000011111111|   377|   00ff|
  #   |    256|0000000100000000|   400|   0100|
  #
  def self.to_digit_table(from = 1, to = 10)
    ret = []
    binary_size = to.to_s(2).size - 1
    binary_pad = (binary_size / 8 + 1) * 8
    oct_size = to.to_s(8).size
    hex_size = to.to_s(16).size - 1
    hex_pad = (hex_size / 4 + 1) * 4
    ret << '|10digit|2digit|8digit|16digit|'
    (from..to).each { |i|ret << "|#{i}|#{i.to_s(2).rjust(binary_pad, '0')}|#{i.to_s(8).rjust(oct_size, '0')}|#{i.to_s(16).rjust(hex_pad, '0')}|" }
    joined = ret.join("\n") + "\n"
    joined.justify_table(:right)
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.150 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.149 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.148 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.147 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.146 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.145 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.144 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.143 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.142 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.141 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.140 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.139 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.138 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.137 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.136 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.135 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.134 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.133 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.132 lib/open_classes/numeric/to_digit_table.rb