[英]How to convert hexadecimal number into a text string (unicode, not just ASCII) (like PHP's hex2bin) in JavaScript?
假設我有一個文本字符串,該文本字符串已用PHP轉換為十六進制。 例如:
<?php
$text = 'hello world';
$text_hex = bin2hex($text);
echo $text_hex; //echos 68656c6c6f20776f726c64
echo '<br>';
echo hex2bin($text_hex); //echos hello world
?>
如何將$text_hex
十六進制數字$text_hex
轉換$text_hex
JavaScript中的文本字符串(就像PHP中的hex2bin()
函數hex2bin()
)。
到目前為止,我只找到了將十六進制數轉換為二進制數的方法,但是沒有找到將二進制數解釋為文本字符串的方法。 因此,例如,我設法做到了:
<script>function hex2bin(hex){ var resultado = (parseInt(hex, 16).toString(2)); return resultado; };</script> <div id="test"> </div> hex2bin, write an hexadecimal number here:<br> <input type="text" id="textinput" oninput="document.getElementById('test').innerText = hex2bin(document.getElementById('textinput').value)"><br> <code> hello world = 68656c6c6f20776f726c64</code>
但是那個二進制數是作為一個數字管理的,我不知道如何將其解釋為文本字符串。
我該怎么辦?
編輯:
非常感謝Bharata和Laurence Mommers提出的解決方案。 它們有效,但僅適用於ASCII字符。 當嘗試使用ñ,ÿ,æ,ç,œ等其他語言時,它不起作用。 我什至嘗試將字符集定義為UTF-8和latin1,以防萬一這是問題所在,但沒有任何改變。
<script>function hex2bin(hex){ var resultado = (parseInt(hex, 16).toString(2)); return resultado; }; function hex2a(hexx) { var hex = hexx.toString();//force conversion var str = ''; for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2) str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); return str; }; function hexToString(hex) { return hex.match(/../g).map(function(v) { // "+" symbol is for converting from string to integer return String.fromCharCode(+('0x'+v)); }).join('') }; </script> Original failed script UTF-8: <div id="test" charset="UTF-8"> </div> Laurence Mommers's script UTF-8: <div id="test1" charset="UTF-8"> </div> Bharata's script UTF-8: <div id="test2" charset="UTF-8"> </div> Original failed script ISO-8859-1: <div id="test3" charset="ISO-8859-1"> </div> Laurence Mommers's script ISO-8859-1: <div id="test4" charset="ISO-8859-1"> </div> Bharata's script ISO-8859-1: <div id="test5" charset="ISO-8859-1"> </div> <br>Write an hexadecimal number here:<br> <input type="text" id="textinput" oninput="document.getElementById('test').innerText = hex2bin(document.getElementById('textinput').value); document.getElementById('test1').innerText = hex2a(document.getElementById('textinput').value); document.getElementById('test2').innerText = hexToString(document.getElementById('textinput').value); document.getElementById('test3').innerText = hex2bin(document.getElementById('textinput').value); document.getElementById('test4').innerText = hex2a(document.getElementById('textinput').value); document.getElementById('test5').innerText = hexToString(document.getElementById('textinput').value)"><br> <code> php bin2hex('hello world') = 68656c6c6f20776f726c64</code> <br> <code> php bin2hex('ñ, ÿ, æ, ç, œ') = c3b12c20c3bf2c20c3a62c20c3a72c20c593</code>
我的猜測是fromCharCode()方法使用的字符集與php使用的字符集不同。 也許有一種方法可以使其使用php使用的字符集? 還是將十六進制轉換為javascript的字符集? 每個瀏覽器的字符集是否不同?
您可以嘗試以下解決方案:
function hexToString(hex) { return hex.match(/../g).map(function(v) { // "+" symbol is for converting from string to integer return String.fromCharCode(+('0x'+v)); }).join('') } var text_hex = '68656c6c6f20776f726c64'; console.log(hexToString(text_hex)); //hello world
這也適用於非ASCII字符。 該decodeURIComponent(escape(x))
解決方案不是最好的解決方案,但這是我知道的唯一轉換為UTF-16的方法
<div id="test"></div> <script> function hex2a(hexx) { var hex = hexx.toString(); // Force string conversion var array = []; for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2) array.push(parseInt(hex.substr(i, 2), 16)); return decodeURIComponent(escape(String.fromCharCode.apply(null, array))); } document.getElementById('test').innerText = hex2a("c3b12c20c3bf2c20c3a62c20c3a72c20c5932c2068656c6c6f20776f726c64"); </script>
php輸出的編碼為utf-8: 0xc3, 0xb1 -> U+00F1 = 'ñ'
。
要在javascript中解碼包含十六進制對序列的字符串,您可以在每對十六進制字符(使用regex)之前插入一個'%'
,然后使用帶有解碼decodeURIComponent
的新字符串進行解析,該字符串將轉義的utf-8序列解碼為字符串字符:
function hex2string(hex)
{
try {
return decodeURIComponent((hex || '').replace(/([0-9A-Fa-f]{2})/g, '%$1'));
}
catch (URIError) {
return '\ufffd';
}
}
這是一個測試hex2string
的代碼片段:
<script> function hex2string(hex) { try { return decodeURIComponent((hex || '').replace(/([0-9A-Fa-f]{2})/g, '%$1')); } catch (URIError) { return '\�'; } } </script> <div id="test"> </div> <br>Write an hexadecimal number here:<br> <input type="text" id="textinput" oninput="document.getElementById('test').innerText = hex2string(document.getElementById('textinput').value)"><br> <code> php bin2hex('hello world') = 68656c6c6f20776f726c64</code> <br> <code> php bin2hex('ñ, ÿ, æ, ç, œ') = c3b12c20c3bf2c20c3a62c20c3a72c20c593</code>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.