今天在把原来用C写的程序移植到javascript上,但是有个地方一直调不通,后来才发现是js奇葩的字符处理出的问题。c中使用的字符处理比如加上一个字符值强制转换一下,在js中就行不通了。

     但是js提供了处理的函数:

字符转ascii码:用charCodeAt();
ascii码砖字符:用fromCharCode();

示例代码:

 1 <script>
 2 str="A";
 3 code = str.charCodeAt();
 4 str2 = String.fromCharCode(code);
 5 str3 = String.fromCharCode(0x60+26);
 6 
 7 document.write(code+'<br />');
 8 document.write(str2+'<br />');
 9 document.write(str3);
10 </script>

 结果显示:

65
A
z65

其实这次的经历也说明这些语言基本是相通的,知识一些细节的不同需要耗费一定时间去转换

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2022-12-23
  • 2021-08-10
  • 2021-08-26
猜你喜欢
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案