JavaScript wchar_t wide character converted to ASCII character code array

Source: Internet
Author: User

String.prototype.charCodeAt

String.fromCharCode ()

String.prototype.toUtfArray = function () {return This.split ("). Reduce (function (A, c) {var code = c.charcodeat (0); A.push (Code >> 8); A.push (code & 0X0FF); return A;}, []);}; String.fromutfarray = function (a) {//length even number of the previous 0if (a.length% 2 ===1) {a.unshift (0);} var wa = [], code = 0;for (var i = 0; i< a.length; i++) {if (i%2===0) {code = A[i] << 8;} else {code |= A[I];WA.P Ush (code);}} Return Wa.map (function (c) {return String.fromCharCode (c);}). Join (');};

  

Test

var S1 = "Hello a"; var a = S1.toutfarray (); [+, 0, 97]console.log, a.tostring ()], var s2 = String.fromutfarray (a); Console.log (S2);  "Hello a"

* UTF-8 to grow longer

Character UTF-8 encoding byte 1 byte 2 byte 3

A 01000001

? 11000011 10010110

Medium 11100100 10111000 10101101

---------------------------------------------------------

Binary Hex Comments
0xxxxxxx 0x00.. 0x7F only byte of a 1-byte character encoding
10xxxxxx 0x80. 0xBF Continuation bytes (1-3 continuation bytes)
110xxxxx 0xC0. 0xDF first byte of a 2-byte character encoding
1110xxxx 0xE0. 0xEF first byte of a 3-byte character encoding
11110xxx 0xF0. 0xf7 first byte of a 4-byte character encoding

  

JavaScript wchar_t wide character converted to ASCII character code array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.