编写一个ruby方法来检查算法的校验和

编写一个ruby方法来检查算法的校验和,ruby,methods,checksum,Ruby,Methods,Checksum,我正在使用K+6+1前缀+代码+校验和格式创建一个字符串。我想写一个方法来检查我的labeland并验证校验和算法是否正确。我不知道如何解决这个问题 require 'benchmark' class Integer def to_bin(width) '%0*b' % [width, self] end end integer_number = 1 binary_number = integer_number.to_bin(24) dictionary =

我正在使用K+6+1前缀+代码+校验和格式创建一个字符串。我想写一个方法来检查我的labeland并验证校验和算法是否正确。我不知道如何解决这个问题

require 'benchmark'

class Integer
    def to_bin(width)
        '%0*b' % [width, self]
    end
end

integer_number = 1
binary_number = integer_number.to_bin(24)
dictionary = '3467ACDEFGHJKMNP'
prefix = 'K'

code = ''
checksum = ''
label = ''

p '================ A1 ================'

Benchmark.bm do |x|
    x.report do
        parity = 0
        binary_number.scan(/\d{4}/).reverse.each_with_index do |item, index|
            word = item.to_i(2)
            parity = index == 0 ? word : parity ^ word
            code += dictionary[item.to_i(2)]
        end

        checksum = dictionary[parity]
        label = prefix + code + checksum
    end
end