Module basic module

module encoding

encodingThe module is a built-in module of FibJS, which is used to convert between various data encoding formats and binary. These data encoding formats include base64, , base32, hex, json, msgpack, multibase, base58etc. some commonly used data encoding formats.

The following is encodinga brief introduction to the various submodules provided by the module:

  • iconv: Used to handle conversion between various encodings.
  • base64: Provides some support for Base64 encoding and decoding, which can encode strings into Base64 format and decode Base64 format into strings.
  • base32: Provides support for Base32 encoding and decoding, which can Base32 encode the given data and return the encoded string. You can also decode Base32 encoded strings into raw data.
  • hex: Provides support for hexadecimal encoding and decoding, which can hexadecimal encode the given data and return the encoded string. It is also possible to decode hex-encoded strings into raw data.
  • json: Provides support for JSON encoding and decoding, which can serialize JavaScript objects into JSON strings and deserialize JSON strings into JavaScript objects.
  • multibase: Provides support for Multibase encoding. Multibase introduces multiple encoding prefix encoding methods based on Base1x encoding.
  • msgpack: Provides support for Msgpack encoding and decoding. Msgpack is a lighter data exchange format than JSON. It can serialize JSON objects into binary data to achieve faster and more efficient data exchange.
  • base58: Provides support for Base58 encoding and decoding. Base58 is a combination of numbers and letters. It does not contain easily confused characters such as numbers 0, letters O, letters I, and letters L, and is less error-prone.

encodingMost of the submodules in the module include encoding and decoding functions. These functions can be used to encode or decode data in a specific format. When using these encoding and decoding modules, you need to select the most suitable module based on the module type and other characteristics to ensure the correctness of encoding and decoding.

encoding` module reference method:

1
var encoding = require('encoding');

object

base32

base32Encoding and decoding module

1
base32 encoding.base32;

base64

base64Encoding and decoding module

1
base64 encoding.base64;

base58

base58Encoding and decoding module

1
base58 encoding.base58;

hex

hexEncoding and decoding module

1
hex encoding.hex;

multibase

multibaseEncoding and decoding module

1
multibase encoding.multibase;

iconv

iconvEncoding and decoding module

1
iconv encoding.iconv;

json

jsonEncoding and decoding module

1
json encoding.json;

msgpack

msgpackEncoding and decoding module

1
msgpack encoding.msgpack;

static function

isEncoding

Determine whether the specified encoding is supported

1
static Boolean encoding.isEncoding(String codec);

Call parameters:

  • codec: String, specifies the encoding format, allowed values ​​are: "hex", "base32", "base58", "base64", "utf8", oriconvCharacter sets supported by the module

Return results:

  • Boolean, returns whether the encoding is supported

encode

WillBufferencode as string

1 2
static String encoding.encode(Buffer data, String codec = "utf8");

Call parameters:

  • data:Buffer, initialize the string, the string will be written in utf-8 format
  • codec: String, specifies the encoding format, allowed values ​​are: "hex", "base32", "base58", "base64", "utf8", oriconvCharacter set supported by the module, the default is "utf8"

Return results:

  • String, returns the encoded string

decode

Decode the string intoBuffer

1 2
static Buffer encoding.decode(String str, String codec = "utf8");

Call parameters:

  • str: String, initialization string, the string will be written in utf-8 format
  • codec: String, specifies the encoding format, allowed values ​​are: "hex", "base32", "base58", "base64", "utf8", oriconvCharacter set supported by the module, the default is "utf8"

Return results:


jsstr

Encode a string into a javascript escaped string for inclusion of text in javascript code

1 2
static String encoding.jsstr(String str, Boolean json = false);

Call parameters:

  • str: String, the string to be encoded
  • json: Boolean, whether to generatejsoncompatible string

Return results:

  • String, returns the encoded string

encodeURI

urlstring security encoding

1
static String encoding.encodeURI(String url);

Call parameters:

  • url: String, to be encodedurl

Return results:

  • String, returns the encoded string

encodeURIComponent

urlPart string security encoding

1
static String encoding.encodeURIComponent(String url);

Call parameters:

  • url: String, to be encodedurl

Return results:

  • String, returns the encoded string

decodeURI

urlSecure string decoding

1
static String encoding.decodeURI(String url);

Call parameters:

  • url: String, to be decodedurl

Return results:

  • String, returns the decoded string