资源加载中... loading...

JavaScript Strategy Writing Instructions

For JavaScript language reasons (JavaScript language built-in string supports ascii and utf-16 encoding only, in order not to lose data), when it encountered string that cannot be encoded, it will return the ArrayBuffer type. All the API interfaces of FMZ that can pass string parameters also support passing the ArrayBuffer type.

This can be briefly illustrated by the following example:

function stringToHex(str) {
    let hex = '';
    for (let i = 0; i < str.length; i++) {
        const charCode = str.charCodeAt(i).toString(16);
        hex += charCode.length === 1 ? '0' + charCode : charCode;
    }
    return hex;
}

function main() {
    const inputString = "abc𠮷123";  // The Unicode code point of the "𠮷" character is more than 16 bits.
    // const inputString = "abcG123"; // If the abcG123 string test is used, the variable outputD is not assigned to ArrayBuffer

    // Use the Encode function to encode inputString as hex.
    const encodedHex = Encode("raw", "string", "hex", inputString);
    Log(encodedHex);  // Contents for: 61 62 63 f0a0aeb7 31 32 33

    // Use your own implementation of the stringToHex function to encode, due to the inability to deal with the "𠮷" character, resulting in an error in the hex encoding.
    const manuallyEncodedHex = stringToHex(inputString);
    Log(manuallyEncodedHex);  // Contents for: 61 62 63 d842dfb7 31 32 33

    // Successful reduction from hex code to string (variable inputString)
    const decodedString = Encode("raw", "hex", "string", encodedHex);
    Log(decodedString);

    // Unable to decode, returns ArrayBuffer, i.e. variable outputD is ArrayBuffer
    const outputD = Encode("raw", "hex", "string", manuallyEncodedHex);
    Log(outputD);

    // Validate the returned ArrayBuffer type variable outputD
    const bufferD = new Uint8Array(outputD);
    let hexBufferD = '';
    for (let i = 0; i < bufferD.length; i++) {
        hexBufferD += bufferD[i].toString(16).padStart(2, '0');
    }
    Log(hexBufferD);    // 61 62 63 d842dfb7 31 32 33
}
C++ Strategy Writing Instructions Web3