Type/to search
Built-in Functions
Global
Version
IsVirtual
GetOS
GetPid
GetMeta
Sleep
Unix
UnixNano
_D
GetCommand
GetLastError
SetErrorFilter
_N
_C
_Cross
JSON.parse
JSON.stringify
Encode
MD5
UUID
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

This function encodes data according to the parameters passed in.

Encode(algo, inputFormat, outputFormat, data)
Encode(algo, inputFormat, outputFormat, data, keyFormat, key)

Examples

  • Example of calling the Encode function.

    javascript
    function main() { Log(Encode("raw", "raw", "hex", "example", "raw", "123")) // Output: 6578616d706c65 Log(Encode("raw", "raw", "hex", "example")) // Output: 6578616d706c65 Log(Encode("sha256", "raw", "hex", "example", "raw", "123")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "", "123")) // Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log(Encode("sha256", "raw", "hex", "example", null, "123")) // Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log(Encode("sha256", "raw", "hex", "example", "string", "123")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("raw", "raw", "hex", "123")) // Output: 313233 Log(Encode("raw", "raw", "base64", "123")) // Output: MTIz Log(Encode("sha256", "raw", "hex", "example", "hex", "313233")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "base64", "MTIz")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba }
    python
    def main(): Log(Encode("raw", "raw", "hex", "example", "raw", "123")) # Output: 6578616d706c65 Log(Encode("raw", "raw", "hex", "example", "", "")) # Output: 6578616d706c65 Log(Encode("sha256", "raw", "hex", "example", "raw", "123")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "", "123")) # Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log(Encode("sha256", "raw", "hex", "example", "string", "123")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("raw", "raw", "hex", "123", "", "")) # Output: 313233 Log(Encode("raw", "raw", "base64", "123", "", "")) # Output: MTIz Log(Encode("sha256", "raw", "hex", "example", "hex", "313233")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "base64", "MTIz")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba
    rust
    fn main() { // In Rust, all 6 parameters of the Encode() function are required; when not encrypting, simply // pass empty strings for keyFormat and key Log!(Encode("raw", "raw", "hex", "example", "raw", "123")); // Output: 6578616d706c65 Log!(Encode("raw", "raw", "hex", "example", "", "")); // Output: 6578616d706c65 Log!(Encode("sha256", "raw", "hex", "example", "raw", "123")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log!(Encode("sha256", "raw", "hex", "example", "", "123")); // Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log!(Encode("sha256", "raw", "hex", "example", "string", "123")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log!(Encode("raw", "raw", "hex", "123", "", "")); // Output: 313233 Log!(Encode("raw", "raw", "base64", "123", "", "")); // Output: MTIz Log!(Encode("sha256", "raw", "hex", "example", "hex", "313233")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log!(Encode("sha256", "raw", "hex", "example", "base64", "MTIz")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba }
  • The parameter algo also supports the following values: "text.encoder.utf8", "text.decoder.utf8", "text.encoder.gbk", "text.decoder.gbk", which are used to encode and decode strings.

    javascript
    function main(){ var ret1 = Encode("text.encoder.utf8", "raw", "hex", "\u4f60\u597d") Log(ret1) // Output: e4bda0e5a5bd var ret2 = Encode("text.decoder.utf8", "hex", "string", ret1) Log(ret2) var ret3 = Encode("text.encoder.gbk", "raw", "hex", "\u4f60\u597d") Log(ret3) // Output: c4e3bac3 var ret4 = Encode("text.decoder.gbk", "hex", "string", ret3) Log(ret4) }
    python
    def main(): ret1 = Encode("text.encoder.utf8", "raw", "hex", "\u4f60\u597d", "", "") Log(ret1) # Output: e4bda0e5a5bd ret2 = Encode("text.decoder.utf8", "hex", "string", ret1, "", "") Log(ret2) ret3 = Encode("text.encoder.gbk", "raw", "hex", "\u4f60\u597d", "", "") Log(ret3) # Output: c4e3bac3 ret4 = Encode("text.decoder.gbk", "hex", "string", ret3, "", "") Log(ret4)
    rust
    fn main() { // In Rust, all 6 parameters of the Encode() function are required; when not encrypting, pass // empty strings for keyFormat and key let ret1 = Encode("text.encoder.utf8", "raw", "hex", "\u{4f60}\u{597d}", "", ""); Log!(ret1); // Output: e4bda0e5a5bd let ret2 = Encode("text.decoder.utf8", "hex", "string", &ret1, "", ""); Log!(ret2); let ret3 = Encode("text.encoder.gbk", "raw", "hex", "\u{4f60}\u{597d}", "", ""); Log!(ret3); // Output: c4e3bac3 let ret4 = Encode("text.decoder.gbk", "hex", "string", &ret3, "", ""); Log!(ret4); }

Returns

TypeDescription

string

Encoded and encrypted data.

Arguments

NameTypeRequiredDescription

algo

string

Yes

The algorithm for encoding, hashing, encryption or compression, for example raw, sha256, text.encoder.utf8 or aes.encrypt; see the remarks for all values.

inputFormat

string

Yes

Used to specify the data format of the data parameter. The inputFormat parameter supports being set to one of "raw", "hex", "base64", "string". "raw" indicates raw data, "hex" indicates hex-encoded data, "base64" indicates base64-encoded data, and "string" indicates string data. "base64.url" and "base64.rawurl" (URL-safe base64, the latter without padding) and "latin1" (each character's code point is one byte) are also supported.

outputFormat

string

Yes

Used to specify the output data format. The outputFormat parameter supports being set to one of "raw", "hex", "base64", "string". "raw" indicates raw data, "hex" indicates hex-encoded data, "base64" indicates base64-encoded data, and "string" indicates string data. "base64.url" and "base64.rawurl" (URL-safe base64, the latter without padding) are also supported. With "raw" or "string" output, each byte becomes one character (latin1).

data

string

Yes

Data to be processed.

keyFormat

string

No

Used to specify the data format of the key parameter. The keyFormat parameter supports being set to one of "raw", "hex", "base64", "string". "raw" indicates raw data, "hex" indicates hex-encoded data, "base64" indicates base64-encoded data, and "string" indicates string data. "base64.url" and "base64.rawurl" (URL-safe base64, the latter without padding) and "latin1" (each character's code point is one byte) are also supported.

key

string

No

Key used for HMAC encryption.

When the algo parameter is set to "sign" or "signTx", the key parameter must be passed in.

When the algo parameter is set to "raw", the key parameter is not used for HMAC encryption (because HMAC encryption must specify an algorithm).

See Also

Remarks

The Encode() function is only supported in live trading. If the key and keyFormat parameters are not passed in, no key encryption is performed.

Values of algo:

CategoryValues
No algorithmraw
Signingsign, signTx
Hash (MD, SHA-1, SHA-2)md4, md5, sha1, sha224, sha256, sha384, sha512, sha512.384, sha512.256, sha512.224
Hash (SHA-3, Keccak)keccak256, sha3.224, sha3.256, sha3.384, sha3.512, sha3.keccak256, sha3.keccak512
Hash (others)ripemd160, blake2b.256, blake2b.512, blake2s.128, blake2s.256
String encoding and decodingtext.encoder.utf8, text.decoder.utf8, text.encoder.gbk, text.decoder.gbk
Other character setstext.encoder.<charset>, text.decoder.<charset>, for example text.decoder.big5
Ed25519ed25519, ed25519.seed; a hash algorithm can be given, for example ed25519.md5, ed25519.sha512
AES encryption and decryptionaes.encrypt, aes.decrypt, with an optional mode suffix .cbc (default), .cfb, .ecb, for example aes.decrypt.ecb
Compression and decompressiongzip.deflate, gzip.inflate, zlib.deflate, zlib.inflate, raw.deflate, raw.inflate

The AES key is passed in key and must be 16, 24 or 32 bytes long (AES-128, AES-192, AES-256); the CBC and CFB modes use the first 16 bytes of the key as the IV, with PKCS7 padding.