Module basic module

module hex

The hex module is a built-in module that provides a hexadecimal implementation between encoding and decoding binary data and ASCII characters. hexThe module provides two functions: encoding and decoding

Using the encoding method, any binary data can be encoded into a hexadecimal string. For example:

1 2 3 4 5
const hex = require('hex') const data = new Buffer([0x4e, 0x4f, 0x44, 0x45]) // [0x4e, 0x4f, 0x44, 0x45] => 'NODE' const encodedData = hex.encode(data) console.log(encodedData) // "4e4f4445"

Using the decoding method, the hexadecimal string can be decoded into the original binary data. For example:

1 2 3 4 5
const hex = require('hex') const encodedData = '4e4f4445' // 'NODE' const decodedData = hex.decode(encodedData) console.log(decodedData) // [0x4e, 0x4f, 0x44, 0x45]

As you can see, hexit is a very simple encoding and decoding module, which is suitable for some simple conversion needs between binary data and strings.

static function

encode

Encode data in hex format

1
static String hex.encode(Buffer data);

Call parameters:

  • data:Buffer, the data to be encoded

Return results:

  • String, returns the encoded string

decode

Decode string into binary data using hex method

1
static Buffer hex.decode(String data);

Call parameters:

  • data: String, the string to be decoded

Return results:

  • Buffer, returns the decoded binary data