Module: Nis::Util::Convert
- Defined in:
- lib/nis/util/convert.rb
Constant Summary collapse
- HEX_ENCODE_ARRAY =
%w[0 1 2 3 4 5 6 7 8 9 a b c d e f]
Class Method Summary collapse
-
.hex2a(hex) ⇒ String
Convert hex to string.
-
.hex2ua(hex) ⇒ Array
Convert hex to Uint8Array.
-
.hex2ua_rev(hex) ⇒ Array
Reversed convertion of hex to Uint8Array.
-
.rstr2utf8(input) ⇒ String
Converts a raw javascript string into a string of single byte characters using utf8 encoding.
-
.ua2hex(ua) ⇒ string
Convert an Uint8Array to hex.
-
.ua2words(ua, ua_length) ⇒ WordArray
Convert an Uint8Array to WordArray.
-
.utf8_to_hex(str) ⇒ string
Convert UTF-8 to hex.
-
.words2ua(words) ⇒ Uint8Array
Convert a wordArray to Uint8Array.
Instance Method Summary collapse
- #hexlify(obj) ⇒ Object
- #unhexlify(obj) ⇒ Object
-
#utf82rstr(input) ⇒ Object
Does the reverse of rstr2utf8.
Class Method Details
.hex2a(hex) ⇒ String
Convert hex to string
37 38 39 |
# File 'lib/nis/util/convert.rb', line 37 def self.hex2a(hex) hex.scan(/../).map { |el| el.to_i(16).chr }.join end |
.hex2ua(hex) ⇒ Array
Convert hex to Uint8Array
23 24 25 |
# File 'lib/nis/util/convert.rb', line 23 def self.hex2ua(hex) hex.scan(/../).map(&:hex) end |
.hex2ua_rev(hex) ⇒ Array
Reversed convertion of hex to Uint8Array
16 17 18 |
# File 'lib/nis/util/convert.rb', line 16 def self.hex2ua_rev(hex) hex2ua(hex).reverse end |
.rstr2utf8(input) ⇒ String
Converts a raw javascript string into a string of single byte characters using utf8 encoding. This makes it easier to perform other encoding operations on the string.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/nis/util/convert.rb', line 90 def self.rstr2utf8(input) input.unpack('U*').map do |c| case when c < 128 c when 128 < c && c < 2048 [((c >> 6) | 192), ((c & 63) | 128)] else [((c >> 12) | 224), (((c >> 6) & 63) | 128), ((c & 63) | 128)] end end.flatten.map(&:chr).join end |
.ua2hex(ua) ⇒ string
Convert an Uint8Array to hex
30 31 32 |
# File 'lib/nis/util/convert.rb', line 30 def self.ua2hex(ua) ua.map { |el| "#{HEX_ENCODE_ARRAY[el >> 4]}#{HEX_ENCODE_ARRAY[el & 0x0f]}" }.join end |
.ua2words(ua, ua_length) ⇒ WordArray
Convert an Uint8Array to WordArray
60 61 62 63 64 65 66 67 68 |
# File 'lib/nis/util/convert.rb', line 60 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) ⇒ string
Convert UTF-8 to hex
44 45 46 |
# File 'lib/nis/util/convert.rb', line 44 def self.utf8_to_hex(str) rstr2utf8(str).each_byte.map { |b| b.to_s(16) }.join end |
.words2ua(words) ⇒ Uint8Array
Convert a wordArray to Uint8Array
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/nis/util/convert.rb', line 74 def self.words2ua(words) words.map do |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 temp end.flatten end |
Instance Method Details
#hexlify(obj) ⇒ Object
5 6 7 |
# File 'lib/nis/util/convert.rb', line 5 def hexlify(obj) obj.unpack('H*').first end |
#unhexlify(obj) ⇒ Object
9 10 11 |
# File 'lib/nis/util/convert.rb', line 9 def unhexlify(obj) obj.scan(/../).map(&:hex).pack('C*') end |
#utf82rstr(input) ⇒ Object
Does the reverse of rstr2utf8.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/nis/util/convert.rb', line 104 def utf82rstr(input) # let output = "", i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0; # while (i < input.length) { # c = input.charCodeAt(i); # if (c < 128) { # output += String.fromCharCode(c); # i++; # } else if ((c > 191) && (c < 224)) { # c2 = input.charCodeAt(i + 1); # output += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); # i += 2; # } else { # c2 = input.charCodeAt(i + 1); # c3 = input.charCodeAt(i + 2); # output += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); # i += 3; # } # } # return output; end |