MCPcopy Create free account
hub / github.com/denoland/std / encodeIntoHex

Function encodeIntoHex

encoding/unstable_hex.ts:108–125  ·  view source on GitHub ↗
(
  input: string | Uint8Array_ | ArrayBuffer,
  output: Uint8Array_,
)

Source from the content-addressed store, hash-verified

106 * ```
107 */
108export function encodeIntoHex(
109 input: string | Uint8Array_ | ArrayBuffer,
110 output: Uint8Array_,
111): number {
112 if (typeof input === "string") {
113 input = new TextEncoder().encode(input) as Uint8Array_;
114 } else if (input instanceof ArrayBuffer) {
115 input = new Uint8Array(input);
116 }
117 const min = calcSizeHex((input as Uint8Array_).length);
118 if (output.length < min) {
119 throw new RangeError("Cannot encode input as hex: Output too small");
120 }
121 output = output.subarray(0, min);
122 const i = min - (input as Uint8Array_).length;
123 output.set(input as Uint8Array_, i);
124 return encode(output, i, 0, alphabet);
125}
126
127/**
128 * `decodeHex` takes an input source and decodes it into a

Callers 1

Calls 3

calcSizeHexFunction · 0.90
encodeFunction · 0.90
setMethod · 0.65

Tested by

no test coverage detected