Sumber daya yang dimuat... Pemuatan...

JavaScript Strategi Menulis Instruksi

Untuk alasan bahasa JavaScript (JavaScript bahasa built-in mendukung stringasciidanutf-16mengkodekan hanya, untuk tidak kehilangan data), ketika ia menemukan string yang tidak dapat dienkode, itu akan mengembalikanArrayBuffersemua antarmuka API FMZ yang dapat melewati string parameter juga mendukung melewatiArrayBuffer type.

Hal ini dapat diilustrasikan secara singkat dengan contoh berikut:

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++ Strategi Menulis Instruksi Web3