@technobuddha > library > Binary > Encoding
Function: encodeBinary()
ts
function encodeBinary(input: Uint8Array, encoding: BinaryEncoding): string;Defined in: encode-binary.ts:30
Encode an BinaryObject into a string
The string can be in base64, base64url, hex, or binary format.
base64: The binary object is encoded using encodeBase64base64url: The binary object is encoded using encodeBase64Urlhex: each byte in the binary object is converted to a 2-digit hexadecimal number.binary: each byte in the binary object is converted to a 8-bit character.
Parameters
| Parameter | Type | Description |
|---|---|---|
input | Uint8Array | binary object |
encoding | BinaryEncoding | The encoding to use |
Returns
string
Encoded string
Example
typescript
encodeBinary(Uint8Array([72, 101, 108, 108, 111]), 'base64'); // 'SGVsbG8='
encodeBinary(Uint8Array([72, 101, 108, 108, 111]), 'base64url'); // 'SGVsbG8'
encodeBinary(Uint8Array([72, 101, 108, 108, 111]), 'hex'); // '48656c6c6f'
encodeBinary(Uint8Array([72, 101, 108, 108, 111]), 'binary'); // 'Hello'Remarks
A string encoded in binary format may not be "well-formed"