Hex(adecimal) Encode / Decode Helpers - From Hex(adecimal) String to Bin(ary) String And Back

hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass, Kernel and more

Part of the If I Were ~~King~~ Matz (aka Yukihiro Matsumoto) - Ideas For Ruby 4.0 - What's Broken & Missing in Ruby 3.x and How To Fix It Series

Usage

For some background on working with bin(ary) and hex(adecimal) strings in ruby see the Programming Bits, Bytes 'n' Blocks Step-by-Step Book / Guide Let's start with the three types of strings, that is, bytes, string buffers, and frozen strings, ...

To make programming hex(adecimal) strings easier let's add some encode / decode helpers.

Kernel - decode_hex (or hex) and encode_hex

require 'hexutils'

# get a bin(ary) string from a hex(adecimal) string
hex '0xffff'     # or
hex( '0xffff' )  # or
decode_hex( '0xffff' )
#=> "\xff\xff".b

# get a hex(a)decimal string from a bin(ary) string
encode_hex( "\xff\xff".b )
#=> "ffff"

Note: The decode_hex decode_hex (or hex) allows all variants of upper- and lowercase with or without the 0x/0X prefix e.g. '0XFFFF', 'FFFF', 'ffff', etc. For convenience whitespace incl. newlines are allowed anywhere.

String - to_hex (or hex) and hex? (or is_hex?)

# get a hex(a)decimal string from a bin(ary) string
"\xff\xff".b.hex   # or
"\xff\xff".b.to_hex   # or
#=> "ffff"

"\xff\xff".b.hex?  # or
"\xff\xff".b.is_hex?
#=> false

"ffff".hex?      # or
"ffff".is_hex?   # or
"0xffff".is_hex?   # or
"0XFFFF".is_hex?   # or
#=> true

NOTE: The "old" String#hex method that returns a integer number from a hex(adecimal) string gets "monkey patched" or is that rewired? to String#old_hex and String#hexnum.

Bonus: NilClass - to_hex (or hex) and hex? (or is_hex?)

nil.hex?  # or
nil.is_hex?
#=> false

nil.hex   # or
nil.to_hex
#=> ''

That's it for now.

License

The scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.

Questions? Comments?

Send them along to the wwwmake forum. Thanks!