1

Topic: js problems with the coding

Good afternoon! A problem of a following sort, it is necessary for me from a line with Russian and  letters to receive an array byte. On open spaces,  the following code for implementation of this. Then to lead certain manipulations (it is not so important . and without it the problem is shown) and to tear this array byte in the same coding that I turned from a following code expected to see  - ProverkaTest, but received - ПроверкаTest Flew on other coding ISO-8859-1. I so understood is guilty encodeURI but how to solve a problem differently function getBytes (str) {var bytes = [], char; str = encodeURI (str); while (str.length) {char = str.slice (0, 1); str = str.slice (1); if (' % '! == char) {bytes.push (char.charCodeAt (0));} else {char = str.slice (0, 2); str = str.slice (2); bytes.push (parseInt (char, 16));}} return bytes;} function ab2str (buf) {return String.fromCharCode.apply (null, new Uint8Array (buf));} var x = getBytes ("ProverkaTest");//x = [208,159,209,128,208,190,208,178,208,181,209,128,208,186,208,176,84,101,115,116] var y = ab2str (x);//y = ПроверкаTest alert (y); https://jsfiddle.net/6706b41L/

2

Re: js problems with the coding

Hello, codenet, you wrote: a C> Good afternoon! A unique output to tear reversely I see function ab2str (buf) {var result = ""; for (var i = 0; i <buf.length; i ++) {if (buf [i] <127 || buf [i] === 0) result + = String.fromCharCode (buf [i]); else {result + = ("%" + buf [i].toString (16) + "%" + buf [i+1].toString (16)); i ++;}} return decodeURI (result);} but the code looks very sadly. Can eat more correct approach?

3

Re: js problems with the coding

Hello, codenet, you wrote: a C> Good afternoon! The C> from a following code I expected to see  - ProverkaTest And what for you on bytes cut? function ab2str (a, xz = '? ') {var i, z, n, q, r, err=0, res = ", len=a.length; for (i=0; i <len; i ++) {z=a [i]; if ((z&0x80) == 0) r=z; else {n=0; q=0x80; do {n ++; q>> =1;} while (z&q); if (n <2) err=1; else {r=z& (q-1); if (r == 0) err=2; else {for (; n> 1; n-) {if (++ i> =len) {err=3; break;} z=a [i]; if ((z&0xC0)! =0x80) {err=4; break;} r = (r <<6) | (z&0x3F);}}}} if (err) {err=0; res + = xz;} else res + = String.fromCharCode (r);} return res;} https://github.com/ashtuchkin/iconv-lite

4

Re: js problems with the coding

Hello, kov_serg, you wrote: _> Hello, codenet, you wrote: a C>> Good afternoon! The C>> from a following code I expected to see  - ProverkaTest _> And what for you on bytes cut? _> _> function ab2str (a, xz = '? ') {_> var i, z, n, q, r, err=0, res = ", len=a.length; _> for (i=0; i <len; i ++) {_> z=a [i]; if ((z&0x80) == 0) r=z; else {_> n=0; q=0x80; do {n ++; q>> =1;} while (z&q); _> if (n <2) err=1; else {_> r=z& (q-1); if (r == 0) err=2; else {_> for (; n> 1; n-) {_> if (++ i> =len) {err=3; break;} _> z=a [i]; if ((z&0xC0)! =0x80) {err=4; break;} _> r = (r <<6) | (z&0x3F); _>} _>} _>} _>} _> if (err) {err=0; res + = xz;} else res + = String.fromCharCode (r); _>} _> return res; _>} _> _> https://github.com/ashtuchkin/iconv-lite Thanks! On bytes cut before enciphering of messages, yes it was possible to use ssl and it is not soared, but  so do not arrive