Code Monkey home page Code Monkey logo

hex_string's Introduction

Description

HexString provides two String extension methods:

  • String#to_hex_string explodes a string of binary data into human-readable hex tuples, and
  • String#to_byte_string converts a string of human-readable hex digits into the corresponding bytes.

Examples

# Convert data to human-readable hex tuples:
>> "hello".to_hex_string
=> "68 65 6c 6c 6f"

# Compact a hex string into its data equivalent:
>> "77 6f 72 6c 64".to_byte_string
=> "world"

# (#to_byte_string is space and case-insensitive:)
>> "776F726C64".to_byte_string
=> "world"

# Peek at the first 4 bytes of an executable on OS X:
>> File.read("/bin/ls")[0..3].to_hex_string
=> "ca fe ba be"

# Omit spaces in hex string output:
>> "hello".to_hex_string(false)
=> "68656c6c6f"

Motivation

When working with binary message or file formats, we often want to have a peek at some segment of binary data and talk about individual byte values in human-relatable terms.

This sort of thing comes in handy during testing, debugging and data introspection, especially when it's inconvenient or impractical to capture the desired binary data to file in order to view it with a hex editor or other binary file reader.

We were inspired to publish this humble Gem after we found ourselves copying it by hand from project to project over the course of several years.

Authors

hex_string's People

Contributors

baconpat avatar dcrosby42 avatar patshaughnessy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.