通过File.sysread()读取单个byte或多个bytes
在进行一些字符串对比时是无法直接对比的
source_file = File.open("E:/test.pcap","rb")
destination_file = File.new("E:/output.pcap","wb")
while true
begin
by = source_file.sysread(1) #表示一个字节一个字节的读取
p by
p by.class
p by == "\xD4"
destination_file.syswrite(by)
rescue => e
puts e.message
break
end
end
source_file.close
destination_file.close
运行的结果是:
"\xD4"
String
false
解决的办法是,先拷贝一份byte(保证原数据不被破坏),将这份byte强制用UTF-8编码,即可进行对比
cby = "#{by}".force_encoding("UTF-8")
p cby
p cby.class
p cby == "\xD4"
对应的运行结果是:
"\xD4"
String
true