Sha256: dbae43b3f5c4e83d87635dc733fccf08959b70098ac88957efe270467d4f5f86

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

module Orchestrator
    module Transcoder
        # Converts a hex encoded string into a binary string
        #
        # @param data [String] a hex encoded string
        # @return [String]
        def hex_to_byte(data)
            # Removes invalid characters
            data = data.gsub(/(0x|[^0-9A-Fa-f])*/, "")

            # Ensure we have an even number of characters
            data.prepend('0') if data.length % 2 > 0

            # Breaks string into an array of characters
            output = ""
            data.scan(/.{2}/) { |byte| output << byte.hex}
            return output
        end
        
        # Converts a binary string into a hex encoded string
        #
        # @param data [String] a binary string
        # @return [String]
        def byte_to_hex(data)
            output = ""
            data.each_byte { |c|
                s = c.to_s(16)
                s.prepend('0') if s.length % 2 > 0
                output << s
            }
            return output
        end
        
        # Converts a string into an array of bytes
        #
        # @param data [String] data to be converted to bytes
        # @return [Array]
        def str_to_array(data)
            data.bytes.to_a
        end
        
        # Converts a byte array into a binary string
        #
        # @param data [Array] an array of bytes
        # @return [String]
        def array_to_str(data)
            data.pack('c*')
        end


        # Makes the functions private when included
        module_function :hex_to_byte
        module_function :byte_to_hex
        module_function :str_to_array
        module_function :array_to_str
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
orchestrator-1.0.2 lib/orchestrator/utilities/transcoder.rb
orchestrator-1.0.1 lib/orchestrator/utilities/transcoder.rb