module Nem::Util::Convert

Constants

HEX_ENCODE_ARRAY

Public Class Methods

bin2hex(bin) click to toggle source

@param [Array] bin @return [String]

# File lib/nem/util/convert.rb, line 41
def self.bin2hex(bin)
  bin.map { |v| '%02x' % v }.join
end
hex2a(hex) click to toggle source

Convert hex to string @param [String] hex @return [String]

# File lib/nem/util/convert.rb, line 23
def self.hex2a(hex)
  hex.scan(/../).inject('') { |memo, el| memo << el.hex.chr }
end
hex2bin(hex) click to toggle source

@param [Array] bin @return [String]

# File lib/nem/util/convert.rb, line 29
def self.hex2bin(hex)
  hex2ua(hex).pack('C*')
end
hex2bin_rev(hex) click to toggle source

@param [Array] bin @return [String]

# File lib/nem/util/convert.rb, line 35
def self.hex2bin_rev(hex)
  hex2ua_rev(hex).pack('C*')
end
hex2ua(hex) click to toggle source

Convert hex to Uint8Array @param [String] hex @return [Array]

# File lib/nem/util/convert.rb, line 16
def self.hex2ua(hex)
  hex.scan(/../).map(&:hex)
end
hex2ua_rev(hex) click to toggle source

Reversed convertion of hex to Uint8Array @param [String] hex @return [Array]

# File lib/nem/util/convert.rb, line 9
def self.hex2ua_rev(hex)
  hex2ua(hex).reverse
end
hex_to_utf8(hex_str) click to toggle source

Convert hex to UTF-8 @param [string] str @return [string]

# File lib/nem/util/convert.rb, line 55
def self.hex_to_utf8(hex_str)
  [hex_str].pack('H*').force_encoding('UTF-8')
end
rstr2utf8(str) click to toggle source

@param [String] str @return [String]

# File lib/nem/util/convert.rb, line 98
def self.rstr2utf8(str)
  str.unpack('U*').inject('') do |memo, c|
    memo << case
            when c < 128
              c.chr
            when 128 < c && c < 2048
              (c >> 6 | 192).chr + (c & 63 | 128).chr
      else
              (c >> 12 | 224).chr + (c >> 6 & 63 | 128).chr + (c & 63 | 128).chr
    end
  end
end
ua2hex(ua) click to toggle source

Convert an Array to hex @param [Array] ua - An Uint8Array @return [string]

# File lib/nem/util/convert.rb, line 62
def self.ua2hex(ua)
  ua.inject('') { |memo, el| memo << "#{HEX_ENCODE_ARRAY[el >> 4]}#{HEX_ENCODE_ARRAY[el & 0x0f]}" }
end
ua2words(ua, ua_length) click to toggle source

Convert an Uint8Array to WordArray @param [Array] ua - An Uint8Array @param [number] uaLength - The Uint8Array length @return [WordArray]

# File lib/nem/util/convert.rb, line 70
def self.ua2words(ua, ua_length)
  ua[0, ua_length].each_slice(4).map do |chunk|
    x = chunk[0] * 0x1000000 +
      chunk[1] * 0x10000 +
      chunk[2] * 0x100 +
      chunk[3] * 0x1
    x > 0x7fffffff ? x - 0x100000000 : x
  end
end
utf8_to_hex(str) click to toggle source

Convert UTF-8 to hex @param [string] str @return [string]

# File lib/nem/util/convert.rb, line 48
def self.utf8_to_hex(str)
  rstr2utf8(str).bytes.inject('') { |memo, b| memo << b.to_s(16) }
end
words2ua(words) click to toggle source

Convert a wordArray to Uint8Array @param [Array] destUa - A destination Uint8Array @param [Array] cryptowords - A wordArray @return [Array]

# File lib/nem/util/convert.rb, line 84
def self.words2ua(words)
  words.inject([]) do |memo, v|
    temp = []
    v += 0x100000000 if v < 0
    temp[0] = (v >> 24)
    temp[1] = (v >> 16) & 0xff
    temp[2] = (v >> 8) & 0xff
    temp[3] = (v) & 0xff
    memo + temp
  end
end