目录

Text Decoding

API Declaration

No declaration is required.

Modules Import

Add the following configuration to the script element on the page where the API will be called:

import decode from '@system.decode'

Or

const decode = require('system.decode')

Application Scope

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

API

Description

TextDecoder(ENCODING, OPTIONS).decode(ARRAYBUFFER)

Decodes the byte stream into a string.

[h2]TextDecoder(ENCODING, OBJECT) (1080+)

Description

Creates a TextDecoder object.

ENCODING

Parameter

Type

Mandatory

Description

encoding

string

Yes

Decoder encoding format.

OBJECT

Parameter

Type

Mandatory

Description

fatal

boolean

No

Indicates whether to display fatal errors.

ignoreBOM

boolean

No

Indicates whether to ignore BOM.

Sample Code

let encoding = "utf-8"
let options = {
    "fatal":true,
    "ignoreBOM":false,
}const decoder = new TextDecoder(encoding, options)

[h2]TextDecoder. decode(ARRAYBUFFER) (1080+)

Description

Decodes the ArrayBuffer into a string.

ARRAYBUFFER

Parameter

Type

Mandatory

Description

arrayBuffer

byte array

Yes

ArrayBuffer to be decoded.

Sample Code

let encoding = "utf-8"
let options = {
    "fatal":true,
    "ignoreBOM":false,
}
const decoder = new TextDecoder(encoding, options)
let arrayBuffer = new Uint8Array([81, 117, 105, 99, 107, 32, 97, 112, 112])
const result = decoder.decode(arrayBuffer)
console.log(result) // The result is 'Quick App'.

Demo

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <text if="{{display}}" style="width: 700px;font-size:50px">beforedecoder:{{ value }}</text>
    <text if="{{display}}" style="width: 700px;font-size:50px;color: red">decodervalue:{{ decodervalue }}</text>
    <input class="input" type="button" value="Decoder" onclick="Decoder" />
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .input {
    background-color: #0faeff;
    color: #ffffff; 
    width:400px;
    height: 100px;
    font-size: 50px;
    border-radius:15px;
  }
</style>

<script>
  module.exports = {
    data: {
      componentData: {},
      display: false,
      value: '',
      decodervalue: ''
    },
     onInit() {
      this.$page.setTitleBar({
        text: 'text decoder',
        textColor: '#ffffff',
        backgroundColor: '#007DFF',
        backgroundOpacity: 0.5,
        menu: true
      });
    },
    Decoder() {
      let encoding = "utf-8"
      let options = {
        "fatal": true,
        "ignoreBOM": false,
      }
      const decoder = new TextDecoder(encoding, options)
      this.value = new Uint8Array([81, 117, 105, 99, 107, 32, 97, 112, 112])
      this.decodervalue = decoder.decode(this.value)
      this.display = true
      console.log(this.decodervalue) // The result is "Quick app";
    }
  }
</script>

The following figure shows the final effect.

Version Change History

Version

Date

Description

1080

2021-12-28

Added the decoding module in which the byte arrays can be decoded to strings.