js中ASCII码和字符互相转换的方法

十进制ASCII码转换成字符

  1. 使用String.fromCodePoint(num1[, ...[, numN]])方法
String.fromCharCode(65) // A
String.fromCharCode(90) // Z
String.fromCharCode(97) // a
String.fromCharCode(122) // z
  1. 也可以使用String.fromCharCode(num1[, ...[, numN]])方法
String.fromCodePoint(65) // A
String.fromCodePoint(90) // Z
String.fromCodePoint(97) // a
String.fromCodePoint(122) // z

fromCodePoint(), fromCharCode()两个都是String的静态方法,所以直接使用,不需要实例化。
两者的主要区别是:
fromCharCode() 出现的早,可以处理常用的字符编码
fromCodePoint() ES2015出现的,可以处理高位编码。

字符转换成十进制ASCII码

'A'.charCodeAt() // 65
'a'.charCodeAt() // 97
'Z'.charCodeAt() // 90
'z'.charCodeAt() // 122

参考:

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt

转载于:https://www.cnblogs.com/weiqinl/p/10575727.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值