* Returns bytes from given binary string. * @param {string} string Binary string * @return {Uint8Array} Bytes
(string)
| 73 | * @return {Uint8Array} Bytes |
| 74 | */ |
| 75 | static bytesFromBinaryString (string) { |
| 76 | string = StringUtil.removeWhitespaces(string) |
| 77 | |
| 78 | // Fill up with trailing zeros |
| 79 | if (string.length % 8 > 0) { |
| 80 | string = string.padEnd(string.length - (string.length % 8) + 8, '0') |
| 81 | } |
| 82 | |
| 83 | // Decode each byte |
| 84 | const bytes = StringUtil.chunk(string, 8).map((byteString, index) => { |
| 85 | const byte = parseInt(byteString, 2) |
| 86 | if (byteString.match(/[0-1]{8}/) === null || isNaN(byte)) { |
| 87 | throw new ByteEncodingError( |
| 88 | `Invalid binary encoded byte '${byteString}'`) |
| 89 | } |
| 90 | return byte |
| 91 | }) |
| 92 | |
| 93 | return new Uint8Array(bytes) |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns base64 string representing given bytes. |
no test coverage detected