Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

汉字与Unicode码的相互转化 #272

Open
yanyue404 opened this issue Apr 11, 2024 · 0 comments
Open

汉字与Unicode码的相互转化 #272

yanyue404 opened this issue Apr 11, 2024 · 0 comments

Comments

@yanyue404
Copy link
Owner

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function toUnicodeFun(data) {
        if (data == '' || typeof data == 'undefined') return '请输入汉字'
        var str = ''
        for (var i = 0; i < data.length; i++) {
          let s = data.charCodeAt(i).toString(16).toUpperCase()
          str += '\\u' + s
        }
        return str
      }
      let code = toUnicodeFun('请修改')

      console.log('unicode:', code)

      function toChineseWords(data) {
        if (data == '' || typeof data == 'undefined')
          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
      }

      let zh = toChineseWords(code)
      console.log('zh:', zh)
    </script>
  </body>
</html>

参考

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

No branches or pull requests

1 participant