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

Method bytesFromHexString

src/ByteEncoder.js:38–57  ·  view source on GitHub ↗

* Returns bytes from given hex string. * @param {string} string Hex string * @return {Uint8Array} Bytes

(string)

Source from the content-addressed store, hash-verified

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.

Callers 3

ByteEncoder.jsFile · 0.80
inputValueDidChangeMethod · 0.80
viewTextDidChangeMethod · 0.80

Calls 3

removeWhitespacesMethod · 0.80
matchMethod · 0.80
chunkMethod · 0.45

Tested by

no test coverage detected