|
| 1 | +/* |
| 2 | + * Copyright 2019 NEM |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import * as utilities from './Utilities'; |
| 17 | + |
| 18 | +export class Convert { |
| 19 | + |
| 20 | + /** |
| 21 | + * Decodes two hex characters into a byte. |
| 22 | + * @param {string} char1 The first hex digit. |
| 23 | + * @param {string} char2 The second hex digit. |
| 24 | + * @returns {number} The decoded byte. |
| 25 | + */ |
| 26 | + public static toByte = (char1: string, char2: string): number => { |
| 27 | + const byte = utilities.tryParseByte(char1, char2); |
| 28 | + if (undefined === byte) { |
| 29 | + throw Error(`unrecognized hex char`); |
| 30 | + } |
| 31 | + return byte; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Determines whether or not a string is a hex string. |
| 36 | + * @param {string} input The string to test. |
| 37 | + * @returns {boolean} true if the input is a hex string, false otherwise. |
| 38 | + */ |
| 39 | + public static isHexString = (input: string): boolean => { |
| 40 | + if (0 !== input.length % 2) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + for (let i = 0; i < input.length; i += 2) { |
| 44 | + if (undefined === utilities.tryParseByte(input[i], input[i + 1])) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + } |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Converts a hex string to a uint8 array. |
| 53 | + * @param {string} input A hex encoded string. |
| 54 | + * @returns {Uint8Array} A uint8 array corresponding to the input. |
| 55 | + */ |
| 56 | + public static hexToUint8 = (input: string): Uint8Array => { |
| 57 | + if (0 !== input.length % 2) { |
| 58 | + throw Error(`hex string has unexpected size '${input.length}'`); |
| 59 | + } |
| 60 | + const output = new Uint8Array(input.length / 2); |
| 61 | + for (let i = 0; i < input.length; i += 2) { |
| 62 | + output[i / 2] = Convert.toByte(input[i], input[i + 1]); |
| 63 | + } |
| 64 | + return output; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Reversed convertion hex string to a uint8 array. |
| 69 | + * @param {string} input A hex encoded string. |
| 70 | + * @returns {Uint8Array} A uint8 array corresponding to the input. |
| 71 | + */ |
| 72 | + public static hexToUint8Reverse = (input: string): Uint8Array => { |
| 73 | + if (0 !== input.length % 2) { |
| 74 | + throw Error(`hex string has unexpected size '${input.length}'`); |
| 75 | + } |
| 76 | + const output = new Uint8Array(input.length / 2); |
| 77 | + for (let i = 0; i < input.length; i += 2) { |
| 78 | + output[output.length - 1 - (i / 2)] = Convert.toByte(input[i], input[i + 1]); |
| 79 | + } |
| 80 | + return output; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Converts a uint8 array to a hex string. |
| 85 | + * @param {Uint8Array} input A uint8 array. |
| 86 | + * @returns {string} A hex encoded string corresponding to the input. |
| 87 | + */ |
| 88 | + public static uint8ToHex = (input) => { |
| 89 | + let s = ''; |
| 90 | + for (const byte of input) { |
| 91 | + s += utilities.Nibble_To_Char_Map[byte >> 4]; |
| 92 | + s += utilities.Nibble_To_Char_Map[byte & 0x0F]; |
| 93 | + } |
| 94 | + |
| 95 | + return s; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Converts a uint8 array to a uint32 array. |
| 100 | + * @param {Uint8Array} input A uint8 array. |
| 101 | + * @returns {Uint32Array} A uint32 array created from the input. |
| 102 | + */ |
| 103 | + public static uint8ToUint32 = (input) => new Uint32Array(input.buffer); |
| 104 | + |
| 105 | + /** |
| 106 | + * Converts a uint32 array to a uint8 array. |
| 107 | + * @param {Uint32Array} input A uint32 array. |
| 108 | + * @returns {Uint8Array} A uint8 array created from the input. |
| 109 | + */ |
| 110 | + public static uint32ToUint8 = (input: Uint32Array): Uint8Array => new Uint8Array(input.buffer); |
| 111 | + |
| 112 | + /** Converts an unsigned byte to a signed byte with the same binary representation. |
| 113 | + * @param {number} input An unsigned byte. |
| 114 | + * @returns {number} A signed byte with the same binary representation as the input. |
| 115 | + * |
| 116 | + */ |
| 117 | + public static uint8ToInt8 = (input: number): number => { |
| 118 | + if (0xFF < input) { |
| 119 | + throw Error(`input '${input}' is out of range`); |
| 120 | + } |
| 121 | + return input << 24 >> 24; |
| 122 | + } |
| 123 | + |
| 124 | + /** Converts a signed byte to an unsigned byte with the same binary representation. |
| 125 | + * @param {number} input A signed byte. |
| 126 | + * @returns {number} An unsigned byte with the same binary representation as the input. |
| 127 | + */ |
| 128 | + public static int8ToUint8 = (input: number): number => { |
| 129 | + if (127 < input || -128 > input) { |
| 130 | + throw Error(`input '${input}' is out of range`); |
| 131 | + } |
| 132 | + return input & 0xFF; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Converts a raw javascript string into a string of single byte characters using utf8 encoding. |
| 137 | + * This makes it easier to perform other encoding operations on the string. |
| 138 | + * @param {string} input - A raw string |
| 139 | + * @return {string} - UTF-8 string |
| 140 | + */ |
| 141 | + public static rstr2utf8 = (input: string): string => { |
| 142 | + let output = ''; |
| 143 | + |
| 144 | + for (let n = 0; n < input.length; n++) { |
| 145 | + const c = input.charCodeAt(n); |
| 146 | + |
| 147 | + if (128 > c) { |
| 148 | + output += String.fromCharCode(c); |
| 149 | + } else if ((127 < c) && (2048 > c)) { |
| 150 | + output += String.fromCharCode((c >> 6) | 192); |
| 151 | + output += String.fromCharCode((c & 63) | 128); |
| 152 | + } else { |
| 153 | + output += String.fromCharCode((c >> 12) | 224); |
| 154 | + output += String.fromCharCode(((c >> 6) & 63) | 128); |
| 155 | + output += String.fromCharCode((c & 63) | 128); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return output; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Convert UTF-8 to hex |
| 164 | + * @param {string} input - An UTF-8 string |
| 165 | + * @return {string} |
| 166 | + */ |
| 167 | + public static utf8ToHex = (input: string): string => { |
| 168 | + const rawString = Convert.rstr2utf8(input); |
| 169 | + let result = ''; |
| 170 | + for (let i = 0; i < rawString.length; i++) { |
| 171 | + result += rawString.charCodeAt(i).toString(16); |
| 172 | + } |
| 173 | + return result; |
| 174 | + } |
| 175 | +} |
0 commit comments