MCPcopy Create free account
hub / github.com/cryptii/cryptii / bytesFromBinaryString

Method bytesFromBinaryString

src/ByteEncoder.js:75–94  ·  view source on GitHub ↗

* Returns bytes from given binary string. * @param {string} string Binary string * @return {Uint8Array} Bytes

(string)

Source from the content-addressed store, hash-verified

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.

Callers 1

viewTextDidChangeMethod · 0.80

Calls 3

removeWhitespacesMethod · 0.80
matchMethod · 0.80
chunkMethod · 0.45

Tested by

no test coverage detected