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.91 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.90 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.89 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.88 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.87 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.86 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.85 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.84 lib/open_classes/numeric/to_digit_table.rb
tbpgr_utils-0.0.83 lib/open_classes/numeric/to_digit_table.rb