js中ASCII碼和字符互相轉(zhuǎn)換的方法
十進制ASCII碼轉(zhuǎn)換成字符
- 使用
String.fromCodePoint(num1[, ...[, numN]])方法
String.fromCharCode(65) // A
String.fromCharCode(90) // Z
String.fromCharCode(97) // a
String.fromCharCode(122) // z
- 也可以使用
String.fromCharCode(num1[, ...[, numN]])方法
String.fromCodePoint(65) // A
String.fromCodePoint(90) // Z
String.fromCodePoint(97) // a
String.fromCodePoint(122) // z
fromCodePoint(), fromCharCode()兩個都是String的靜態(tài)方法,所以直接使用,不需要實例化。
兩者的主要區(qū)別是:
fromCharCode() 出現(xiàn)的早,可以處理常用的字符編碼
fromCodePoint() ES2015出現(xiàn)的,可以處理高位編碼。
字符轉(zhuǎn)換成十進制ASCII碼
'A'.charCodeAt() // 65
'a'.charCodeAt() // 97
'Z'.charCodeAt() // 90
'z'.charCodeAt() // 122
參考:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
感謝您的認真閱讀,更多內(nèi)容請查看:
出處:http://www.rzrgm.cn/weiqinl
個人主頁http://weiqinl.com
github: weiqinl
簡書:weiqinl
您的留言討論是對博主最大的支持!
本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
出處:http://www.rzrgm.cn/weiqinl
個人主頁http://weiqinl.com
github: weiqinl
簡書:weiqinl
您的留言討論是對博主最大的支持!
本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。

浙公網(wǎng)安備 33010602011771號