byte to string consistently with java

by Leo

HTML

<script src="https://cdn.jsdelivr.net/npm/[email protected]/text-encoder-lite.js"></script>

JavaScript

//try to use "windows-1251"
////https://cdn.jsdelivr.net/npm/[email protected]/text-encoder-lite.js !!! polyfill does not work with windows-1251
//var decoderClass =  typeof TextDecoder !== 'undefined' ? TextDecoder : window.TextDecoderLite;
//let decoder = new decoderClass('windows-1251');
//let bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]);
//alert(decoder.decode(bytes));


// "Привет, мир!"
var decoderClass =  typeof TextDecoder !== 'undefined' ? TextDecoder : window.TextDecoderLite;
let decoder = new decoderClass('utf-8');
let bytes1 = new Uint8Array(
   [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]
); 
alert(decoder.decode(bytes1) + " ----- It is " + (decoder.decode(bytes1) == "Привет, мир!"));

// "漢語"
let bytes2 = new Uint8Array(
   [230, 188, 162, 232, 170, 158]
);
alert(decoder.decode(bytes2) + " ----- It is " + (decoder.decode(bytes2) == "漢語"));



// ==== code in java  ========
//import java.io.UnsupportedEncodingException;
//import java.nio.charset.*;
//
//public class A
//{
//    public static void main(String[] args) throws UnsupportedEncodingException
//    {
//        //207 - 256, 240 - 256, 232 - 256, 226 - 256, 229 - 256, 242 - 256, 44, 32, 236 - 256, 232 - 256, 240 - 256, 33
//        /* === В JS ===
//        let decoder = new TextDecoder('windows-1251');
//        let bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]);
//        console.log(decoder.decode(bytes)); // Привет, мир! */
//        test("Привет, мир!".getBytes("windows-1251"), Charset.forName("windows-1251"));
//
//        //208 - 256, 159 - 256, 209 - 256, 128 - 256, 208 - 256, 184 - 256, 208 - 256, 178 - 256, 208 - 256, 181 - 256, 209 - 256, 130 - 256, 44, 32, 208 - 256, 188 - 256, 208 - 256, 184 - 256, 209 - 256, 128 - 256, 33
//        /* === В JS ===
//        let decoder = new TextDecoder('utf-8');
//        let bytes = new Uint8Array([208, 159,...