data_valid?
我有两个调用方法的测试用例。第一个返回false
,第二个返回true
,为什么?
55: def data_valid? d
56: crc = d[-1]
57: data = d[1..-2]
58: len = d[0]
=> 59: binding.pry
60: (data ^ len) == crc
61: end
2.0.0 (#<MicroAeth::Message:0x007fbefc3ceae8>):0 > (data ^ len) == crc
=> false
2.0.0 (#<MicroAeth::Message:0x007fbefc3ceae8>):0 > (data ^ len)
=> "\xB1"
2.0.0 (#<MicroAeth::Message:0x007fbefc3ceae8>):0 > crc
=> "\xB1"
2.0.0 (#<MicroAeth::Message:0x007fbefc3ceae8>):0 > exit
have a good day!
F
From: /Users/rudolph9/Projects/CombustionEmissionsTesting/micro_aeth.rb @ line 59 MicroAeth::Message#data_valid?:
55: def data_valid? d
56: crc = d[-1]
57: data = d[1..-2]
58: len = d[0]
=> 59: binding.pry
60: (data ^ len) == crc
61: end
2.0.0 (#<MicroAeth::Message:0x007fbefe83a8c8>):0 > (data ^ len) == crc
=> true
2.0.0 (#<MicroAeth::Message:0x007fbefe83a8c8>):0 > (data ^ len)
=> "+"
2.0.0 (#<MicroAeth::Message:0x007fbefe83a8c8>):0 > crc
=> "+"
以下是我比较自定义 XOR 方法的返回的 String 类的扩展^
。
class ::String
###
# @return the first charater in the string as an integer
def byte
self.bytes[0]
end
###
# XOR two strings
# @str assumed to be a one byte string or integer
def ^ str
if str.class == String
str = str.byte
elsif str.class == Fixnum
nil
else
raise "invalid arg: #{str.class} \n Must be String or Fixnum"
end
self.bytes.each do |i|
str = str ^ i
end
str.chr
end
end
我相信这与第一次比较非 ASCII 字符有关。如何正确设置条件?