Let us say that I have a text string that was converted into hexadecimal with PHP. For example:
<?php
$text = 'hello world';
$text_hex = bin2hex($text);
echo $text_hex; //echos 68656c6c6f20776f726c64
echo '<br>';
echo hex2bin($text_hex); //echos hello world
?>
How do I convert $text_hex
, which will be a hexadecimal number, into a text string in JavaScript (just like the function hex2bin()
in PHP).
So far I have only found ways to convert the hexadecimal number into a binary number, but not ways to interpret that binary number as a text string. So, for example, I managed to make this:
<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>
But that binary number is managed as a number, and I don't get how to interpret it as a text string.
How could I do it?
Edit:
Thank you very much to Bharata and to Laurence Mommers for their proposed solutions. They work but only with ascii characters. When it is tried to use others like ñ, ÿ, æ, ç, œ, it does not work. I tried even defining the charset as UTF-8 and as latin1 just in case that was the issue but it is not changing anything.
<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>
My guess is that the method fromCharCode() is using a different charset to the one that php is using. Perhaps there is a way to make it use the charset that php uses? Or to make the hexadecimal conversion to the charset of javascript? Does this charset varies per browser?