首先,讓我解釋一下,我幾乎不知道這是什麼意思,所以如果我說什麼愚蠢的,請讓我知道。在紅寶石文件中生成CRC16校驗和
我正在研究一個將xml字符串和文件轉換爲十六進制字符串以嵌入到智能卡並通過網絡發送的系統。這一點工作得很好。
部分這個過程,但是,涉及到也生成的字符串/文件,我已經給出了一些C#示例代碼的校驗(遺憾的截圖,它是從一個PDF文件中取出):
我的工作是將此代碼轉換爲Ruby,但我只取得了部分成功。
我一直在考慮一些樣本字符串和圖像,用以測試,所有這些都可以在這個規範
require 'spec_helper'
require 'checksum_calculator'
describe ChecksumCalculator do
describe '.call' do
subject { described_class.call(data) }
context 'when passed xml' do
let(:data) { '<Card><RegNo>00000003</><Title>Mr</><Initial>B</><Surname>Black</><PrintDate>01-Feb-2004</><ExpiryDate>Dec 2012</><Colour>Black</><Scheme>CSCS</><Type>Senior Manager</><IssuedBy>Construction Skills</><Foil>H+S Test</><Top>CONSTRUCTION SKILLS CERTIFICATION SCHEME</><Bottom>The registered holder of this card holds the~construction qualifications listed on the reverse</><Back><Line>NVQ Construction Site Management L5</></Back><LogoID>3</></Card>' }
it 'returns "8B5A"' do
expect(subject).to eq '8B5A'
end
end
context 'when passed Lorem ipsum' do
let(:data) { 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut consectetur justo, in semper enim. Curabitur placerat varius diam. Suspendisse dapibus dui sapien, ac mattis mi aliquam sit amet. Phasellus interdum magna et urna rutrum elementum. Fusce odio risus, laoreet vel velit sit amet, convallis egestas metus. Nunc tincidunt, ligula ut lobortis facilisis, nulla sem semper dui, eget consectetur ligula mi eu felis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas ipsum metus, feugiat eget pretium in, ultricies sit amet leo. Mauris sed facilisis turpis, quis porta sapien. In hac habitasse platea dictumst. Integer gravida, tortor nec pharetra ornare, nulla ipsum sagittis risus, nec cursus justo lacus vel sapien. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Quisque quis sem at ante posuere sodales non sit amet dolor. Mauris sagittis magna sed aliquam lacinia. Phasellus nec sapien maximus, fringilla urna a, commodo mauris.' }
it 'returns "9034"' do
expect(subject).to eq '9034'
end
end
context 'when passed "Hello World"' do
let(:data) { 'Hello World' }
it 'returns "B2DA"' do
expect(subject).to eq 'B2DA'
end
end
context 'when passed "CSCS Data"' do
let(:data) { 'CSCS Data' }
it 'returns "9583"' do
expect(subject).to eq '9583'
end
end
context 'when passed ""' do
let(:data) { '' }
it 'returns "829E"' do
expect(subject).to eq '829E'
end
end
context 'when passed 2k_michael_knight.jpg' do
let(:data) { File.read(File.join(Rails.root, 'spec', 'fixtures', '2k_michael_knight.jpg')) }
it 'returns "5404"' do
expect(subject).to eq '5404'
end
end
context 'when passed 2k_lego_dude.jpg' do
let(:data) { File.read(File.join(Rails.root, 'spec', 'fixtures', '2k_lego_dude.jpg')) }
it 'returns "53005"' do
expect(subject).to eq '53005'
end
end
end
end
而且在這裏找到是我寫的代碼(它使用digest-crc gem):
require 'digest/crc16_ccitt'
class ChecksumCalculator
def self.call(data)
crc = Digest::CRC16CCITT.new
crc << data
(crc.checksum^0xffff).to_s(16).upcase
end
end
,但同時對於字符串的規格是路過就好,圖像都沒有。
Failures:
1) ChecksumCalculator.call when passed 2k_lego_dude.jpg returns "53005" Failure/Error: expect(subject).to eq '53005'
expected: "53005"
got: "CF0D"
(compared using ==)
# ./spec/lib/checksum_calculator_spec.rb:58:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
2) ChecksumCalculator.call when passed 2k_michael_knight.jpg returns "5404" Failure/Error: expect(subject).to eq '5404'
expected: "5404"
got: "151C"
(compared using ==)
# ./spec/lib/checksum_calculator_spec.rb:51:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
我有一種感覺,代碼工作正常,但我在圖像中傳遞的方式有問題。誰能幫忙?
僅供參考,以下是圖像:
- https://dl.dropboxusercontent.com/u/2724815/2k_lego_dude.jpg
- https://dl.dropboxusercontent.com/u/2724815/2k_michael_knight.jpg
謝謝
非常感謝您的回答。我回到了供應商(誰提供了我們上面的十進制值),他們現在已經證實我們產生的十六進制值實際上是正確的。 –