Topic: Converting fixed length binary to ASCII format

I am looking for the best way within Ruby, or maybe a gem, to convert a binary file that is fixed length into the representative ASCII format.  Some example data includes:

- Holdtime (integer - 4 bytes)

ASCII:  00:04
Binary:  00000100000000000000000000000000

- Work Code (char - 17 bytes)

ASCII:  12345
Binary:  0011000100110010001100110011010000110101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Any insight on how best to approach this would be greatly appreciated.  Thanks!

Re: Converting fixed length binary to ASCII format

Figured this out as well of course:

  #Method for parsing the various datatypes from the ECH file
  def dump_binary type, length
    case type
    when 'int'
      #Process integers, assigning appropriate profile based on length
      #such as long int, short int and tiny int.
      case length
      when 4
        value = @binary_file.read(length).unpack("l").first.to_i
      when 2
        value = @binary_file.read(length).unpack("s").first.to_i
      when 1
        value = @binary_file.read(length).unpack("U").first.to_i
      end
    #Process appropriate intergers into datetime format in the database
    when 'datetime'
      case length
      when 4
        value = @binary_file.read(length).unpack("l").first.to_i
      end
    #Process strings
    when 'str'
      value = @binary_file.read(length).unpack("M").first.to_s.rstrip
    #Process individual bits that are booleans
    when 'bool'
      value = @binary_file.read(length).unpack("b8").last.to_s
    #Process that one wierd boolean that is actually an int, instead of a bit
    when 'bool_int'
      value = @binary_file.read(length).unpack("U").first.to_i
    end
    return value
  end

Which iterates through the file based on a YML set of field definitions:

    while @binary_file.eof == FALSE do 
      @log.debug '<====================START RECORD====================>'
      @echi_schema["fields"].each do | field |
        #We handle the 'boolean' fields differently, as they are all encoded as bits in a single 8-bit byte
        if field["type"] == 'bool'
          if bool_cnt == 0
            value = dump_binary field["type"], field["length"]
          end
          @log.debug field ["name"] + "{ type => #{field["type"]} & length => #{field["length"]} } value => " + value.slice(bool_cnt, 1)
          bool_cnt += 1
          if bool_cnt == 8
            bool_cnt = 0
          end
        else
          #Process 'standard' fields
          value = dump_binary field["type"], field["length"]
          @log.debug field["name"] + " { type => #{field["type"]} & length => #{field["length"]} } value => " + value.to_s
        end
      record_cnt += 1
    end