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
-
#utf82rstr(input) ⇒ Object
Does the reverse of rstr2utf8.
Class Method Details
.hex2a(hex) ⇒ String
Convert hex to string
29 30 31 |
# File 'lib/nis/util/convert.rb', line 29 def self.hex2a(hex) hex.scan(/../).map { |el| el.to_i(16).chr }.join end |
.hex2ua(hex) ⇒ Array
Convert hex to Uint8Array
15 16 17 |
# File 'lib/nis/util/convert.rb', line 15 def self.hex2ua(hex) hex.scan(/../).map(&:hex) end |
.hex2ua_rev(hex) ⇒ Array
Reversed convertion of hex to Uint8Array
8 9 10 |
# File 'lib/nis/util/convert.rb', line 8 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.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nis/util/convert.rb', line 82 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
22 23 24 |
# File 'lib/nis/util/convert.rb', line 22 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
52 53 54 55 56 57 58 59 60 |
# File 'lib/nis/util/convert.rb', line 52 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
36 37 38 |
# File 'lib/nis/util/convert.rb', line 36 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
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nis/util/convert.rb', line 66 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
#utf82rstr(input) ⇒ Object
Does the reverse of rstr2utf8.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/nis/util/convert.rb', line 96 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 |