18. Utils
The utils object is a built-in object that provides utility functions.
Functional overview:
- Supports UTF-8 encoding/decoding.
utils Global Object
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
utils.stringToUtf8bin() | Encodes string data in UTF-8. | 01.00.00+ | |
utils.utf8binToString() | Decodes binary data in UTF-8. | 01.00.00+ |
Details
utils.stringToUtf8bin(data)
Encodes string data in UTF-8.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
data | string | mandatory | String data | |
return | ArrayBuffer | - | Encoded binary data | When an error occurs, an exception is raised. |
utils.utf8binToString(data[, offset[, length]])
Decodes binary data in UTF-8.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
data | ArrayBuffer, Array[ArrayBuffer] | mandatory | Binary data'ArrayBuffer' or Array of 'ArrayBuffer' can be specified.If Array of 'ArrayBuffer' is specified, concatenates the binary data in the order of the array, then starts the decoding process. | |
offset | number | optional | Offset of decode processing start position If array of 'ArrayBuffer' is specified for 'data', it will be an offset over all 'data' after the concatenation of the binary data.The default value is 0. | If 'length' is used, it cannot be omitted. |
length | number | optional | Decode length [byte] The default value is calculated by subtracting the decode start position offset from the total size of 'data'. A size less than or equal to the default value must be specified. | It can be omitted even if 'offset' is used. |
return | {DecodedData}, undefined | - | {DecodedData} : Decoded data If the decoding fails, 'undefined' will be returned. | When an error occurs, an exception is raised. |
{DecodedData}
Name | Type | Summary | Note |
---|---|---|---|
.str | string | Decoded string data | |
.bin | ArrayBuffer | When leftover data from 1 to 3 bytes exists at the end of the binary data to be decoded, the leftover data portion is output as binary data. If leftover data does not exist, it will be 'undefined'. |
Object Usage Examples
Sample 1
This is a sample of split decoding using offset for data encoded in UTF-8.
This example utilizes the offset and length of .utf8binToString().
The possibility of splitting multi-byte characters without prior consideration of their cutoff points is shown.
var testStr_1 = 'Thank you Teşekkür ありがとう';
print(testStr_1);
var testEnc_1 = utils.stringToUtf8bin(testStr_1);
print(ab2Hex_00(testEnc_1));
var chunk = 7;
for(var n = 0; n < 20; n++) {
var testDec_1 = utils.utf8binToString(testEnc_1, 0, chunk + n);
print(('00' + n).substr(-2) + '_1:' + testDec_1['str']);
//if(testDec_1['bin']) print(ab2Hex_00(testDec_1['bin']));
var rest = testDec_1['bin'];
var len = 0;
if(rest) len = rest.byteLength;
var testDec_2 = utils.utf8binToString(testEnc_1, chunk + n - len);
print(('00' + n).substr(-2) + '_2:' + testDec_2['str']);
//if(testDec_2['bin']) print(ab2Hex_00(testDec_2['bin']));
}
function ab2Hex_00(ab) {
ret_str = '';
var a = new Uint8Array(ab);
for(var i = 0; i < a.length; i++) {
ret_str += ('00' + a[i].toString(16).toUpperCase()).substr(-2);
}
return ret_str;
}
Sample 2
This is a sample that splits the UTF-8 encoded data into two parts and decodes them individually.
This can be utilized when the target of decoding is stream data.
The possibility of splitting multi-byte characters without prior consideration of their cutoff points is shown.
var testStr_1 = 'Thank you Teşekkür ありがとう';
print(testStr_1);
var testEnc_1 = utils.stringToUtf8bin(testStr_1);
print(ab2Hex_00(testEnc_1));
var chunk = 7;
for(var n = 0; n < 20; n++) {
var chunk_1 = testEnc_1.slice(0, chunk + n);
var chunk_2 = testEnc_1.slice(chunk + n);
var testDec_1 = utils.utf8binToString(chunk_1);
print(('00' + n).substr(-2) + '_1:' + testDec_1['str']);
//if(testDec_1['bin']) print(ab2Hex_00(testDec_1['bin']));
var rest = testDec_1['bin'];
var testDec_2 = utils.utf8binToString([rest, chunk_2]);
print(('00' + n).substr(-2) + '_2:' + testDec_2['str']);
//if(testDec_2['bin']) print(ab2Hex_00(testDec_2['bin']));
}
function ab2Hex_00(ab) {
ret_str = '';
var a = new Uint8Array(ab);
for(var i = 0; i < a.length; i++) {
ret_str += ('00' + a[i].toString(16).toUpperCase()).substr(-2);
}
return ret_str;
}
Updated: 2021-08-23