Skip to content

Commit

Permalink
dec2hex: better fix without padding
Browse files Browse the repository at this point in the history
  • Loading branch information
noraj committed Mar 11, 2022
1 parent bbac81e commit 84153b4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/ctf_party/hex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ 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
# adding prefix must be done after case change, complex conditional to avoid cropping when odd byte lenght
out = (out.size.odd? ? [out[0]] + out[1..].scan(/.{1,2}/) : out.scan(/.{2}/)).map do |x|
opts[:prefixall] + x
end.join
return opts[:prefix] + out
end

Expand Down

0 comments on commit 84153b4

Please sign in to comment.