Skip to content

[js] 第287天 写一个汉字与Unicode码的互转的方法 #1830

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第287天 写一个汉字与Unicode码的互转的方法

我也要出题

Activity

Zhou-Bill

Zhou-Bill commented on Jan 28, 2020

@Zhou-Bill
function encode(string) {
    let result = []
    for(let i = 0; i < string.length; i++) {
        result[i] = ("00" + string.charCodeAt(i).toString(16)).slice(-4);
    }
    return "\\u" + result.join("\\u")
}

function decode(string) {
    return unescape(string)
}
changed the title [-][js] 第287天 写一个汉字与Unicode码的相转的方法[/-] [+][js] 第287天 写一个汉字与Unicode码的互转的方法[/+] on Jan 29, 2020
xiaoqiangz

xiaoqiangz commented on Aug 31, 2022

@xiaoqiangz

function tounicode(data)
{
if(data == '') return '请输入汉字';
var str ='';
for(var i=0;i<data.length;i++)
{
str+="\u"+parseInt(data[i].charCodeAt(0),10).toString(16);
}
return str;
}
function tohanzi(data)
{
if(data == '') return '请输入十六进制unicode';
data = data.split("\u");
var str ='';
for(var i=0;i<data.length;i++)
{
str+=String.fromCharCode(parseInt(data[i],16).toString(10));
}
return str;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@Zhou-Bill@xiaoqiangz

        Issue actions

          [js] 第287天 写一个汉字与Unicode码的互转的方法 · Issue #1830 · haizlin/fe-interview