module Orchestrator::Transcoder

Public Class Methods

array_to_str(data) click to toggle source

Converts a byte array into a binary string

@param data [Array] an array of bytes @return [String]

# File lib/orchestrator/utilities/transcoder.rb, line 49
def array_to_str(data)
    return data if data.is_a? String
    data.pack('c*')
end
byte_to_hex(data) click to toggle source

Converts a binary string into a hex encoded string

@param data [String] a binary string @return [String]

# File lib/orchestrator/utilities/transcoder.rb, line 24
def byte_to_hex(data)
    data = array_to_str(data) if data.is_a? Array

    output = ""
    data.each_byte { |c|
        s = c.to_s(16)
        s.prepend('0') if s.length % 2 > 0
        output << s
    }
    return output
end
hex_to_byte(data) click to toggle source

Converts a hex encoded string into a binary string

@param data [String] a hex encoded string @return [String]

# File lib/orchestrator/utilities/transcoder.rb, line 7
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}
    output.pack('c*')
end
str_to_array(data) click to toggle source

Converts a string into an array of bytes

@param data [String] data to be converted to bytes @return [Array]

# File lib/orchestrator/utilities/transcoder.rb, line 40
def str_to_array(data)
    return data if data.is_a? Array
    data.bytes.to_a
end

Private Instance Methods

array_to_str(data) click to toggle source

Converts a byte array into a binary string

@param data [Array] an array of bytes @return [String]

# File lib/orchestrator/utilities/transcoder.rb, line 49
def array_to_str(data)
    return data if data.is_a? String
    data.pack('c*')
end
byte_to_hex(data) click to toggle source

Converts a binary string into a hex encoded string

@param data [String] a binary string @return [String]

# File lib/orchestrator/utilities/transcoder.rb, line 24
def byte_to_hex(data)
    data = array_to_str(data) if data.is_a? Array

    output = ""
    data.each_byte { |c|
        s = c.to_s(16)
        s.prepend('0') if s.length % 2 > 0
        output << s
    }
    return output
end
hex_to_byte(data) click to toggle source

Converts a hex encoded string into a binary string

@param data [String] a hex encoded string @return [String]

# File lib/orchestrator/utilities/transcoder.rb, line 7
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}
    output.pack('c*')
end
str_to_array(data) click to toggle source

Converts a string into an array of bytes

@param data [String] data to be converted to bytes @return [Array]

# File lib/orchestrator/utilities/transcoder.rb, line 40
def str_to_array(data)
    return data if data.is_a? Array
    data.bytes.to_a
end