nodemcu-firmware/docs/en/modules/encoder.md

76 lines
1.5 KiB
Markdown
Raw Normal View History

# encoder Module
The encoder modules provides various functions for encoding and decoding byte data.
## encoder.toBase64()
Provides a Base64 representation of a (binary) Lua string.
#### Syntax
`b64 = encoder.toBase64(binary)`
#### Parameters
`binary` input string to Base64 encode
#### Return
A Base64 encoded string.
#### Example
```lua
print(encoder.toBase64(cyrpto.hash("sha1","abc")))
```
## encoder.fromBase64()
Decodes a Base64 representation of a (binary) Lua string back into the original string.
#### Syntax
`binary_string = encoder.toBase64(b64)`
#### Parameters
`b64` Base64 encoded input string
#### Return
The decoded Lua (binary) string.
#### Example
```lua
print(encoder.fromBase64(encoder.toBase64("hello world")))
```
## encoder.toHex()
Provides an ASCII hex representation of a (binary) Lua string. Each byte in the input string is represented as two hex characters in the output.
#### Syntax
`hexstr = encoder.toHex(binary)`
#### Parameters
`binary` input string to get hex representation for
#### Returns
An ASCII hex string.
#### Example
```lua
print(encoder.toHex(crypto.hash("sha1","abc")))
```
## encoder.fromHex()
Provides a Lua binary string decode of a ASCII hex string. Each byte in the output string is represented as two hex characters in the input.
#### Syntax
`binary = encoder.toHex(hexstr)`
#### Parameters
`hexstr` An ASCII hex string.
#### Returns
Decoded string of hex representation.
#### Example
```lua
print(encoder.fromHex("6a6a6a")))
```