js 字符串与16进制互转代码
时间: 2023-08-09 09:05:39 浏览: 80
将字符串转成16进制:
```javascript
function stringToHex(str) {
let hex = "";
for(let i=0; i<str.length; i++) {
let charCode = str.charCodeAt(i).toString(16);
hex += charCode.length < 2 ? "0" + charCode : charCode;
}
return hex;
}
```
将16进制转成字符串:
```javascript
function hexToString(hex) {
let str = "";
for(let i=0; i<hex.length; i+=2) {
let charCode = parseInt(hex.substr(i, 2), 16);
str += String.fromCharCode(charCode);
}
return str;
}
```
使用示例:
```javascript
let str = "hello world";
let hex = stringToHex(str);
console.log(hex); // 68656c6c6f20776f726c64
console.log(hexToString(hex)); // hello world
```
相关问题
字符串与16进制数互转表
在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 ]
js字符串转16进制
在JavaScript中,可以使用charCodeAt()方法和toString(16)方法将字符串转换为16进制表示。以下是一个示例代码:
```javascript
function stringToHex(str){
var val = "";
for(var i = 0; i < str.length; i++){
if(val == ""){
val = str.charCodeAt(i).toString(16);
}else{
val += "," + str.charCodeAt(i).toString(16);
}
}
return val;
}
var str = "赵兄托我帮你办点事";
var hex = stringToHex(str);
console.log(hex); // 8d75,5144,6258,6211,5e2e,4f60,529e,70b9,4e8b
```
在上面的代码中,我们定义了一个stringToHex函数,它接受一个字符串作为参数,并将其转换为16进制表示。在循环中,我们使用charCodeAt()方法来获取字符串中每个字符的Unicode值,并使用toString(16)方法将其转换为16进制。将每个16进制值拼接起来,用逗号隔开,最后返回结果。
请注意,上述代码仅作为示例,你可以根据自己的需求进行修改和扩展。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* [JavaScript中字符串与16进制之间的转换](https://blog.csdn.net/weixin_30404405/article/details/96973810)[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^v92^chatsearchT0_1"}} ] [.reference_item]
- *2* [js字符串与十六进制相互转换](https://blog.csdn.net/weixin_30609331/article/details/98463565)[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^v92^chatsearchT0_1"}} ] [.reference_item]
- *3* [Js字符串与十六进制的相互转换 【转】](https://blog.csdn.net/weixin_34001430/article/details/86117386)[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^v92^chatsearchT0_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文