admin管理员组文章数量:1326345
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?
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?
Share Improve this question edited Jan 18, 2012 at 19:13 Andrew asked Jan 18, 2012 at 18:54 AndrewAndrew 1654 silver badges16 bronze badges 7-
2
unicode
is not initialized, so it isundefined
. In the first iteration, you are basically doingundefined + someNumber
andundefined
is converted toNaN
. – Felix Kling Commented Jan 18, 2012 at 18:57 - charCodeAt returns an integer repsenting the unicode codepoint value. If you add them up as you are, you'll be getting back the equivalent of "1+2+3=6", not "123". – Marc B Commented Jan 18, 2012 at 18:58
- No need to mask your JavaScript block: <!-- --> – Diodeus - James MacFarlane Commented Jan 18, 2012 at 18:58
- 2 A good practice - ending each statement with a semicolon. – Grace Huang Commented Jan 18, 2012 at 18:58
- well now i initialized unicode to 0 but it shows just the unicode of the last character – Andrew Commented Jan 18, 2012 at 19:08
3 Answers
Reset to default 4JavaScript uses UCS-2 internally.
This means that supplementary Unicode symbols are exposed as two separate code units (the surrogate halves). For example,
本文标签:
Converting text to Unicode in javascriptStack Overflow
版权声明:本文标题:Converting text to Unicode in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1742198822a2431577.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'
发表评论