image processing - Comparing bytes in Ruby -
i have binary blob header of either jpg or mp4 file. trying differentiate between two.
when file jpg, first 2 bytes \xff\xd8
. however, when make comparison blob[0] == "\xff"
, fails. when know blob[0]
in fact \xff
what best way this?
this encoding issue. comparing string binary encoding (your jpeg blob) utf-8 encoded string ("\xff"
):
foo = "\xff".force_encoding("binary") # blob bar = "\xff" p foo # => "\xff" p bar # => "\xff" p foo == bar # => false
there several ways create binary encoded string:
str = "\xff\xd8".b # => "\xff\xd8" (ruby 2.x) str.encoding # => #<encoding:ascii-8bit> str = "\xff\xd8".force_encoding("binary") # => "\xff\xd8" str.encoding # => #<encoding:ascii-8bit> str = 0xff.chr + 0xd8.chr # => "\xff\xd8" str.encoding # => #<encoding:ascii-8bit> str = ["ffd8"].pack("h*") # => "\xff\xd8" str.encoding # => #<encoding:ascii-8bit>
all of above can compared blob.
Comments
Post a Comment