typescript - String.fromCharCode vs '0x' 的 TypeScript 错误
问题描述
我需要任何建议来解决这个问题!错误:
('0x' + p1);
错误日志:
TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
代码 :
export function encodeString(str): any {
// first we use encodeURIComponent to get percent-encoded UTF-8,
// then we convert the percent encodings into raw bytes which
// can be fed into btoa.
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
};
解决方案
那这个呢:
return String.fromCharCode(parseInt('0x' + p1, 16));
您需要使用 parseInt(16
表示十六进制)将 UTF-8 代码字符串转换为整数。然后,将它传递给 String.fromCharCode() 函数。
希望能帮助到你。
推荐阅读
- python - 从另一个模块修改一个模块的属性是不好的做法吗?
- git - 如何将更改提交到 Gerrit 中的主分支?
- c# - 使用 Nuget ComponentModel.Annotations 替换程序集引用 ComponentModel.DataAnnotations 的运行时错误
- r - 使用 geom_boxplot() 在整洁的数据框中更改箱线图的顺序
- python - 为每个超过最大值的 X 堆叠“点”
- http - adding 1 user with htpasswd in 2 different servers using ssh connection
- ansible - 使用 Ansible 2.9.6 更改 VMware VM 端口组
- javascript - 应用“无返回等待”时,函数是否应该保持异步?
- sql - 如何在SQL中查找两次之间不存在的数据
- html - NavBar Bootstrap 4,如何不重复隐藏手机屏幕中的某些项目?