lib/hexy.rb in hexy-0.1.2 vs lib/hexy.rb in hexy-0.2.0

- old
+ new

@@ -1,9 +1,11 @@ class Hexy + VERSION = '0.2.0' + def self.dump bytes, config={} Hexy.new(bytes, config).to_s end # @@ -42,11 +44,11 @@ # # b = Hexy.new "\x00\x01\x03(...)", :numbering=>:none, :format=>:fours # puts p.to_s # def initialize bytes, config = {} - @bytes = bytes + @bytes = bytes.force_encoding(Encoding::ASCII_8BIT) @width = config[:width] || 16 @numbering = config[:numbering] == :none ? :none : :hex_bytes @format = case config[:format] when :none, :fours config[:format] @@ -61,12 +63,11 @@ end def to_s str = "" 0.step(@bytes.length, @width) {|i| - string = @bytes[i,@width] - + string = @bytes.byteslice(i,@width).force_encoding(Encoding::ASCII_8BIT) hex = string.unpack("H*")[0] hex.upcase! if @case == :upper if @format == :fours @@ -80,10 +81,11 @@ hex.gsub!(/(\S\S)/) { |m| m+" " } end - string.gsub!(/[\000-\040\177-\377]/, ".") + #string.gsub!(/[\000-\040\177-\377]/, ".") + string.gsub!(/[^\041-\176]/, ".") string.gsub!(/(.{#{@width/2}})/) { |m| m+" " } len = case @format when :fours