* Returns bytes from given hex string. * @param {string} string Hex string * @return {Uint8Array} Bytes
(string)
| 36 | * @return {Uint8Array} Bytes |
| 37 | */ |
| 38 | static bytesFromHexString (string) { |
| 39 | string = StringUtil.removeWhitespaces(string) |
| 40 | |
| 41 | // Fill up bytes with trailing zeros |
| 42 | if (string.length % 2 === 1) { |
| 43 | string = string + '0' |
| 44 | } |
| 45 | |
| 46 | // Decode each byte |
| 47 | const bytes = StringUtil.chunk(string, 2).map((byteString, index) => { |
| 48 | const byte = parseInt(byteString, 16) |
| 49 | if (byteString.match(/[0-9a-f]{2}/i) === null || isNaN(byte)) { |
| 50 | throw new ByteEncodingError( |
| 51 | `Invalid hex encoded byte '${byteString}'`) |
| 52 | } |
| 53 | return byte |
| 54 | }) |
| 55 | |
| 56 | return new Uint8Array(bytes) |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns binary string representing given bytes. |
no test coverage detected