diff --git a/lib/bytes.rb b/lib/bytes.rb index 2386cb1..44d8980 100644 --- a/lib/bytes.rb +++ b/lib/bytes.rb @@ -14,6 +14,7 @@ def bytes_to_str(data) data.unpack('H*').join end +# TODO: remove? def get_byte(data, start = 0, num = 1) data[start...(start + num)].unpack('H*').join.upcase end diff --git a/spec/07_bytes_spec.rb b/spec/07_bytes_spec.rb new file mode 100644 index 0000000..be3e521 --- /dev/null +++ b/spec/07_bytes_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require_relative '../lib/bytes' + +describe 'Bytes', :bytes do + context '#str_hex' do + it 'Should format "\xFF" to "FF"' do + expect(str_hex("\xFF")).to eq('FF') + end + it 'Should format "\x01" to "01"' do + expect(str_hex("\x01")).to eq('01') + end + it 'Should format "\x01\x02" to "01 02"' do + expect(str_hex("\x01\x02")).to eq('01 02') + end + end + context '#str_bytes' do + it 'Should format "ff" to [255]' do + expect(str_bytes('ff')).to eq([255]) + end + it 'Should format "FF" to [255]' do + expect(str_bytes('FF')).to eq([255]) + end + it 'Should format "01" to [1]' do + expect(str_bytes('01')).to eq([1]) + end + it 'Should format "0101" to [1, 1]' do + expect(str_bytes('0101')).to eq([1, 1]) + end + end + context '#bytes_to_str' do + it 'Should format "\xff" to "ff"' do + expect(bytes_to_str("\xff")).to eq('ff') + end + it 'Should format "\x01" to "01"' do + expect(bytes_to_str("\x01")).to eq('01') + end + it 'Should format "\x01\x01" to "0101"' do + expect(bytes_to_str("\x01\x01")).to eq('0101') + end + end +end