I’ve been copying a little utility called HexString around for about three years now. It’s a small extension to Ruby’s String class that provides methods
String#to_byte_string, which generate new strings by either:
- transforming all the bytes in a string into space-delimited string containing hexadecimal tuples, for human consumption
- converting a human-readable string of hexadecimal digits into a string of their corresponding byte values
HexString came into being a few years back when my team was implementing a number of parsers and generators for a proprietary binary file format. In our unit tests, we found it more elegant to express our expectations for certain data segments, and to express binary test inputs, as String literals that could then be automatically converted into their binary equivalent. For test failure messages, we could also convert chunks of binary data back into their hex string equivalent to create succinct, readable test failure messages.
(Usage examples from the README.md file:)
In any application where we found ourselves handling binary data, we found HexString to be a pleasant little helper. A quick search of my hard drive just now reveals over a dozen repositories containing some copy
Shame on me for not publishing this gem earlier, given how often I went looking for a copy.
Don’t get me wrong: HexString is not a fully-fledged binary formatting toolkit. It doesn’t even format or parse multibyte values like ints and shorts. It’s just… handy. Certainly easier than drumming up memories of cryptic calls to