字符串与16进制数互转表
时间: 2023-12-31 11:04:36 浏览: 77
在JavaScript中,可以使用以下代码实现字符串与十六进制数的相互转换:
将字符串转换为十六进制数:
```javascript
function stringToHex(str) {
let hex = '';
for(let i = 0; i < str.length; i++) {
hex += str.charCodeAt(i).toString(16);
}
return hex;
}
let str = "Hello World";
let hex = stringToHex(str);
console.log(hex); // 48656c6c6f20576f726c64
```
将十六进制数转换为字符串:
```javascript
function hexToString(hex) {
let str = '';
for(let i = 0; i < hex.length; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
let hex = "48656c6c6f20576f726c64";
let str = hexToString(hex);
console.log(str); // Hello World
```
这些代码可以将字符串转换为十六进制数,以及将十六进制数转换为字符串。你可以根据需要调用相应的函数来实现转换。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* [javascript实现的字符串与十六进制表示字符串相互转换方法](https://download.csdn.net/download/weixin_38538950/13982205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [毕业设计项目:基于最小错误率和Fisher线性分类器的QT&C++实现手写字符识别系统.zip](https://download.csdn.net/download/qq_35831906/88259404)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文