0

I'm looking for a way to uniformly convert JavaScript chars to binary. One problem that I'm facing is that I'm not able to convert strings used in unescape function correctly

document.write("HI"); ==> HI (case 1)
document.write("\x48\x49");  ==> HI (case 2)
document.write(unescape("%48%49")); ==> HI (case 3)

Case 1: Converting "HI" to binary generates:

0100100001001001

Case 2: Converting "\x48\x49" to binary generates:

0100100001001001

Case 3: Converting "%48%49" to binary generates:

001001010011010000111000001001010011010000111001

I understand what is happening in case 3. it converts each char to binary without realizing that these chars are in hex representation. How can I make case 3 have the same binary representation as case 1 and case 2 without any pre-processing required?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • You are only using Javascript here, so why did you add the Java and Python tags? Javascript isn't Java at all. It is another language. –  Jun 27 '16 at 11:25
  • I'm interested in converting chars in JavaScript code to binary. As for the conversion process, I'm open to use any programming language the is able to convert the above 3 cases uniformly – user5289027 Jun 27 '16 at 11:33

1 Answers1

0

The decodeURIComponent is the right function for this. The decode function is deprecated. Here is an example.

If you put %48%49 in the input and click on the 'Convert' button, it gives you the second case.

var test = document.querySelector("#test");
var btn = document.querySelector("#btn");
var result = document.querySelector("#result");

btn.addEventListener("click", function() {
  var value = decodeURIComponent(test.value);
  var resultText = "";
  
  for (i = 0; i < value.length; i++) {
    resultText += value.charCodeAt(i).toString(2);
  }
  
  result.innerText = resultText;
});
<input type="text" id="test">
<button id="btn">Convert</button>

<p id="result"></p>
  • but this can't handle case 2 correctly. It returns 1011100111100011010011100010111001111000110100111001 for "\x48\x49" – user5289027 Jun 27 '16 at 11:57
  • Yes but this is because when you type `"\x48\x49"`, it transforms at compile time. You can try this in your browser console: type `"\x48\x49"`and it will return "HI" without any other function call. The snippet is just an example to tell you that you should use the `decodeURIComponent` function for what you are trying to achieve. If you want to get HI at runtime you can use the eval function but it is very dangerous. –  Jun 27 '16 at 12:29
  • @user5289027 check out [How do I un-escape a backslash-escaped string in python?](http://stackoverflow.com/questions/1885181/how-do-i-un-escape-a-backslash-escaped-string-in-python) for the second case. – Tadhg McDonald-Jensen Jun 27 '16 at 17:18