简体   繁体   English

Javascript:如何将unicode值转换回字符?

[英]Javascript: How can I convert my unicode values back to characters?

I have been able to successfully convert my characters to Unicode, add 1, however I am having trouble with the final step of converting back to characters. 我已经能够成功地将字符转换为Unicode,添加1,但是在转换回字符的最后一步中遇到了麻烦。 I am not sure why the last statement in the if statement is not working. 我不确定if语句中的最后一条语句为什么不起作用。 Please help! 请帮忙!

 function LetterChanges(str) { for (var i = 0; i < str.length; i++) { var c = str.charCodeAt(i); if (c > 64 && c < 127) { str = str.replace(str.charAt(i), c + 1); str = str.replace(str.charAt(i), str.fromCharCode(i)); } } console.log(str); } LetterChanges("hello*3"); 

fromCharCode is meant to be called on the String object itself as a static method that you pass your instance into. fromCharCode旨在作为传递实例的静态方法在String对象本身上调用。

Additionally, charAt(i) is functionally equivalent to [i] . 另外, charAt(i)在功能上等同于[i]

While I'm not exactly sure what you are after here, you can see the implementation of it here: 虽然我不确定您在这里做什么,但是您可以在此处查看其实现:

 function LetterChanges(str) { for(var i = 0; i < str.length; i++){ var c = str.charCodeAt(i); if(c > 64 && c < 127){ str = str.replace(str[i], String.fromCharCode(c + 1)); } } console.log(str); } LetterChanges("hello*3"); 

You are calling fromCharCode(i) , but i is the index into the string, not the character code. 您正在调用fromCharCode(i) ,但是i是字符串的索引,而不是字符代码。 You want to pass the character code: String.fromCharCode(c+1) or whatever. 您想传递字符代码: String.fromCharCode(c+1)或其他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应 javascript 中将 unicode 转义序列转换为 unicode 字符 - How to convert unicode escape sequences to unicode characters in react javascript 如何将ArrayBuffer转换为11位值并在Javascript中再次返回 - How can I convert an ArrayBuffer to 11-Bits Values and Back again in Javascript 使用 Javascript,如何将二进制字符串(1 和 0)转换回 Unicode 字符? - Using Javascript, how do I convert a binary string (1s and 0s) back into a Unicode character? JavaScript - 如何转换unicode字符?英文数字到波斯数字 - JavaScript - How do I convert unicode characters? English numbers to Persian numbers 如何使用纯 Javascript 将 unicode 字符转换为 HTML 数字实体 - How to convert unicode characters to HTML numeric entities using plain Javascript 如何在 JavaScript 中将 Unicode 十六进制字符转换为 Latin-1 - How to convert Unicode Hex Characters to Latin-1 in JavaScript 如何在 JavaScript 中转换 unicode? - How to convert unicode in JavaScript? 如何将字符串转换为 unicode 字符? - How can I convert a string into a unicode character? 如何将这些ASCI代码转换为Unicode? - How can I convert these ASCI codes to Unicode? 如何检测 Unicode 字符是否在我的 web 页面上正确显示? - How can I detect whether Unicode characters are displayed properly on my web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM