class Bytes

Constants

MAJOR
MINOR
PATCH
VERSION

Public Class Methods

banner() click to toggle source
convert( *args ) click to toggle source
# File lib/bytes.rb, line 31
def self.convert( *args )
  ## used by Bytes() in global Kernel converter method
  if args.size == 1
    if args[0].is_a? Array
      ## assume array of bytes
      ##   to be done
    else  ## assume String
      ## todo/fix: use coerce to_str if arg is NOT a string - why? why not?
      str = args[0]
      ##
      if str.encoding == Encoding::ASCII_8BIT
        ## assume it's binary data - use as is (no hexstring conversion)
        new( str )   ## todo/check: return str as-is (without new) - why? why not?
      else    ## assume it's a hexstring
        from_hex( str )
      end
    end
  else
    ## todo/fix: throw argument error
  end
end
from_hex( hexstr ) click to toggle source
# File lib/bytes.rb, line 16
def self.from_hex( hexstr )
  if ['0x', '0X'].include?( hexstr[0...2] )
    [hexstr[2..-1]].pack('H*')  ## cut-of leading 0x or 0X if present
  else
    [hexstr].pack('H*')
  end
end
new( *args ) click to toggle source
# File lib/bytes.rb, line 12
def self.new( *args )
  String.new( *args ).b
end
root() click to toggle source
# File lib/bytes/version.rb, line 20
def self.root
  "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
end
to_hex( str ) click to toggle source
# File lib/bytes.rb, line 24
def self.to_hex( str )
  # note: unpack returns string with <Encoding:US-ASCII>
  # conver to default encoding
  ##  todo/fix: do NOT hardcode UTF-8 - use default encoding - how?
  str.unpack('H*').first.encode("UTF-8")
end
version() click to toggle source
# File lib/bytes/version.rb, line 12
def self.version
  VERSION
end