Skip to content

@technobuddha > library > Binary > Encoding

Function: decodeBinary()

ts
function decodeBinary(input: string, encoding: BinaryEncoding): Uint8Array;

Defined in: decode-binary.ts:28

Decode a string into a binary object

The string can be in base64, base64url, hex, or binary BinaryEncoding format.

  • base64: The binary object was encoded using encodeBase64
  • base64url: The binary object was encoded using encodeBase64Url
  • hex: each byte in the binary object was converted to a 2-digit hexadecimal number.
  • binary: each byte in the binary object was converted to a single 8-bit character.

Parameters

ParameterTypeDescription
inputstringencoded binary object
encodingBinaryEncodingThe encoding to use

Returns

Uint8Array

encoded string

Example

typescript
decodeBinary('SGVsbG8=', 'base64');      // Uint8Array([72, 101, 108, 108, 111])
decodeBinary('SGVsbG8', 'base64url');    // Uint8Array([72, 101, 108, 108, 111])
decodeBinary('48656c6c6f', 'hex');       // Uint8Array([72, 101, 108, 108, 111])
decodeBinary('Hello', 'binary');         // Uint8Array([72, 101, 108, 108, 111])