# String Encoding and Decoding >![](../../public_sys-resources/icon-note.gif) **NOTE:** >The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ``` import util from '@ohos.util'; ``` ## Required Permissions None ## util.printf printf\(format: string, ...args: Object\[\]\): string Prints the input content in a formatted string. - Parameters

Name

Type

Mandatory

Description

format

string

Yes

Format of the string to print.

...args

Object[]

No

Data to format.

- Return values

Type

Description

string

String in the specified format.

- Example ``` var res = util.printf("%s", "hello world!"); console.log(res); ``` ## util.getErrorString getErrorString\(errno: number\): string Obtains detailed information about a system error code. - Parameters

Name

Type

Mandatory

Description

errno

number

Yes

Error code generated.

- Return values

Type

Description

string

Detailed information about the error code.

- Example ``` var errnum = 10; // 10: a system error number var result = util.getErrorString(errnum); console.log("result = " + result); ``` ## util.callbackWrapper callbackWrapper\(original: Function\): \(err: Object, value: Object\)=\>void Calls back an asynchronous function. In the callback, the first parameter indicates the cause of the rejection \(if the promise has been resolved, the value is **null**\), and the second parameter indicates the resolved value. - Parameters

Name

Type

Mandatory

Description

original

Function

Yes

Asynchronous function.

- Return values

Type

Description

Function

Callback, in which the first parameter indicates the cause of the rejection (the value is null if the promise has been resolved) and the second parameter indicates the resolved value.

- Example ``` async function promiseFn() { return Promise.reject('value'); } var cb = util.callbackWrapper(promiseFn); cb((err, ret) => { expect(err).strictEqual('value'); expect(ret).strictEqual(undefined); }) ``` ## util.promiseWrapper promiseWrapper\(original: \(err: Object, value: Object\) =\> void\): Object Processes an asynchronous function and returns a promise version. - Parameters

Name

Type

Mandatory

Description

original

Function

Yes

Asynchronous function to process.

- Return values

Type

Description

Function

Function in the common error-first style (that is, (err, uses value) =>... is used as the last parameter) and the promise version.

- Example ``` function aysnFun(str1, str2, callback) { if (typeof str1 === 'string' && typeof str1 === 'string') { callback(null, str1 + str2); } else { callback('type err'); } } let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World'); newPromiseObj.then(res => { expect(res).strictEqual('HelloWorld'); }) ``` ## TextDecoder ### Attributes

Name

Type

Readable

Writable

Description

encoding

string

Yes

No

Encoding format.

  • Supported formats: utf-8, ibm866, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-8-i, iso-8859-10, iso-8859-13, iso-8859-14, iso-8859-15, koi8-r, koi8-u, macintosh, windows-874, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, x-mac-cyrilli, gbk, gb18030, big5, euc-jp, iso-2022-jp, shift_jis, euc-kr, utf-16be, utf-16le

fatal

boolean

Yes

No

Whether to display fatal errors.

ignoreBOM

boolean

Yes

No

Whether to ignore the byte order marker (BOM). The default value is false.

### constructor constructor\(encoding?: string, options?: \{ fatal?: boolean; ignoreBOM?: boolean \},\) A constructor used to create a **TextDecoder** instance. - Parameters

Name

Type

Mandatory

Description

encoding

string

No

Encoding format.

options

Object

No

Encoding-related options, which include fatal and ignoreBOM.

**Table 1** options

Name

Type

Mandatory

Description

fatal

boolean

No

Whether to display fatal errors.

ignoreBOM

boolean

No

Whether to ignore the BOM.

- Example ``` var textDecoder = new util.TextDecoder("utf-8",{ignoreBOM:true}); ``` ### decode decode\(input: Uint8Array, options?:\{stream?:false\}\): string Decodes the input parameters and outputs the text. - Parameters

Name

Type

Mandatory

Description

input

Uint8Array

Yes

Uint8Array to decode.

options

Object

No

Options related to decoding.

**Table 2** options

Name

Type

Mandatory

Description

stream

boolean

No

Whether to allow data blocks in the subsequent decode(). Set this parameter to true if data blocks are processed. If data is not divided into blocks or the last data block is processed, set this parameter to false. The default value is false.

- Return values

Type

Description

string

Data decoded.

- Example ``` var textDecoder = new util.TextDecoder("utf-8",{ignoreBOM:true}); var result = new Uint8Array(6); result[0] = 0xEF; result[1] = 0xBB; result[2] = 0xBF; result[3] = 0x61; result[4] = 0x62; result[5] = 0x63; console.log("input num:"); for(var j= 0; j < 6; j++) { console.log(result[j]); } var retStr = textDecoder.decode( result , {stream:false}); console.log("retStr = " + retStr); ``` ## TextEncoder ### Attributes

Name

Type

Readable

Writable

Description

encoding

string

Yes

No

Encoding format. The default value is utf-8.

### constructor constructor\(\) A constructor used to create a **TextEncoder** instance. - Example ``` var textEncoder = new util.TextEncoder(); ``` ### encode encode\(input?: string\): Uint8Array Encodes the input parameter. - Parameters

Name

Type

Mandatory

Description

input

string

Yes

String to encode.

- Return values

Type

Description

Uint8Array

Encoded text.

- Example ``` var textEncoder = new util.TextEncoder(); var result = new Uint8Array(buffer); result = textEncoder.encode("\uD800¥¥"); ``` ### encodeInto encodeInto\(input: string, dest: Uint8Array,\):\{ read: number; written: number \} Stores the UTF-8 encoded text. - Parameters

Name

Type

Mandatory

Description

input

string

Yes

String to encode.

dest

Uint8Array

Yes

Uint8Array instance used to store the UTF-8 encoded text.

- Return values

Type

Description

Uint8Array

Encoded text.

- Example ``` var that = new util.TextEncoder(); var buffer = new ArrayBuffer(4); this.dest = new Uint8Array(buffer); var result = that.encodeInto("abcd", this.dest); ```