// META: global=window,dedicatedworker,shadowrealm
// META: title=Encoding API: Basics
test(function () {
assert_equals((new TextEncoder).encoding, 'utf-8' , 'default encoding is utf-8' );
assert_equals((new TextDecoder).encoding, 'utf-8' , 'default encoding is utf-8' );
}, 'Default encodings' );
test(function () {
assert_array_equals(new TextEncoder().encode(), [], 'input default should be empty string' )
assert_array_equals(new TextEncoder().encode(undefined), [], 'input default should be empty string' )
}, 'Default inputs' );
function testDecodeSample(encoding, string, bytes) {
test(function () {
assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string);
assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer), string);
}, 'Decode sample: ' + encoding);
}
// z (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
// byte-swapped BOM (non-character U+FFFE)
var sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE' ;
test(function () {
var encoding = 'utf-8' ;
var string = sample;
var bytes = [0 x7A, 0 xC2, 0 xA2, 0 xE6, 0 xB0, 0 xB4, 0 xF0, 0 x9D, 0 x84, 0 x9E, 0 xEF, 0 xA3, 0 xBF, 0 xF4, 0 x8F, 0 xBF, 0 xBD, 0 xEF, 0 xBF, 0 xBE];
var encoded = new TextEncoder().encode(string);
assert_array_equals([].slice.call(encoded), bytes);
assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string);
assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer), string);
}, 'Encode/decode round trip: utf-8' );
testDecodeSample(
'utf-16le' ,
sample,
[0 x7A, 0 x00, 0 xA2, 0 x00, 0 x34, 0 x6C, 0 x34, 0 xD8, 0 x1E, 0 xDD, 0 xFF, 0 xF8, 0 xFF, 0 xDB, 0 xFD, 0 xDF, 0 xFE, 0 xFF]
);
testDecodeSample(
'utf-16be' ,
sample,
[0 x00, 0 x7A, 0 x00, 0 xA2, 0 x6C, 0 x34, 0 xD8, 0 x34, 0 xDD, 0 x1E, 0 xF8, 0 xFF, 0 xDB, 0 xFF, 0 xDF, 0 xFD, 0 xFF, 0 xFE]
);
testDecodeSample(
'utf-16' ,
sample,
[0 x7A, 0 x00, 0 xA2, 0 x00, 0 x34, 0 x6C, 0 x34, 0 xD8, 0 x1E, 0 xDD, 0 xFF, 0 xF8, 0 xFF, 0 xDB, 0 xFD, 0 xDF, 0 xFE, 0 xFF]
);
Messung V0.5 in Prozent C=79 H=93 G=86
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-06)
¤
*© Formatika GbR, Deutschland