diff --git a/lib/kaitai/struct/struct.rb b/lib/kaitai/struct/struct.rb index 0b83cc4..d2282d2 100644 --- a/lib/kaitai/struct/struct.rb +++ b/lib/kaitai/struct/struct.rb @@ -531,6 +531,22 @@ def to_signed(x, mask) def self.format_hex(bytes) bytes.unpack('H*')[0].gsub(/(..)/, '\1 ').chop end + + ### + # Guess if the given args are most likely byte arrays. + #

+ # There's no way to know for sure, but {@code Encoding::ASCII_8BIT} is a special encoding that is + # usually used for a byte array(/string), not a character string. For those reasons, that encoding + # is NOT planned to be allowed for human readable texts by KS in general as well. + #

+ # @param args [...] Something to check. + # @see Encoding + # @see List of supported encodings + # + def self.is_byte_array?(*args) + found = args.select { |arg| arg.is_a?(String) and (arg.encoding == Encoding::ASCII_8BIT) } + found.length == args.length + end end ## @@ -569,7 +585,12 @@ def initialize(msg, io, src_path) # "expected", but it turned out that it's not. class ValidationNotEqualError < ValidationFailedError def initialize(expected, actual, io, src_path) - super("not equal, expected #{expected.inspect}, but got #{actual.inspect}", io, src_path) + if Stream.is_byte_array?(expected, actual) + super("not equal, expected [#{Stream.format_hex(expected)}], but got [#{Stream.format_hex(actual)}]", io, src_path) + else + super("not equal, expected #{expected.inspect}, but got #{actual.inspect}", io, src_path) + end + @expected = expected @actual = actual end