Converting text to Unicode in javascript

问题: I have the following: function showUnicode() { var text = prompt( 'Enter the wanted text', 'Unicode' ), unicode = 0, ntext, temp, i = 0 ; /...

问题:

I have the following:

function showUnicode()
{
  var text = prompt( 'Enter the wanted text', 'Unicode' ),
      unicode = 0,
      ntext,
      temp,
      i = 0
  ;

  // got the text now transform it in unicode
  for(i; i < text.length; i++)
  {
    unicode += text.charCodeAt(i)

  }

  // now do an alert
  alert( 'Here is the unicode:n' + unicode + 'nof:n' + text )

}

Thanks for the idea to initialize unicode but now unicode variable gets the Unicode of the last character, why does it?


回答1:

JavaScript uses UCS-2 internally.

This means that supplementary Unicode symbols are exposed as two separate code units (the surrogate halves). For example, '

  • 发表于 2019-01-15 00:23
  • 阅读 ( 199 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除