Skip to content

Commit

Permalink
dec2hex: fix odd byte cropping issue
Browse files Browse the repository at this point in the history
  • Loading branch information
noraj committed Mar 11, 2022
1 parent 866b86b commit bbac81e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## [Unreleased]

- Fix:
- dec2hex: fix odd byte cropping issue

## [2.1.0]

- Chore:
Expand Down
2 changes: 2 additions & 0 deletions lib/ctf_party/hex.rb
Expand Up @@ -59,6 +59,8 @@ def dec2hex(opts = {})
out = ('0' * (opts[:padding] - out.size)) + out if out.size < opts[:padding]
# char case management
out = out.upcase if opts[:case] == :upper
# so scan is safe, scan(/.{1,2}/) doesn't isolate alone byte in right order
out = '0' + out if out.size.odd?
# adding prefix must be done after case change
out = out.scan(/.{2}/).map { |x| opts[:prefixall] + x }.join
return opts[:prefix] + out
Expand Down
2 changes: 2 additions & 0 deletions test/test_string.rb
Expand Up @@ -158,6 +158,8 @@ def test_hex_dec2hex
assert_equal('0000000a', '10'.dec2hex(padding: 8))
assert_equal('0xFF', '255'.dec2hex(prefix: '0x', case: :upper))
assert_equal('\\x6e\\x6f\\x72\\x61\\x6a', '474316169578'.dec2hex(prefixall: '\\x'))
# verify no odd byte cropping issue
assert_equal('1f4a9','128169'.dec2hex)
end

skip def test_hex_dec2hex!
Expand Down

0 comments on commit bbac81e

Please sign in to comment.