0

I saw some hex bytes are converting easily to escaped string e.g. 0x54,0x65 etc but when alphabetic strings coming like 0xAA, 0xAF etc, after converting, its adding extra characters. so how can I configure these charecters to not generating these extra characters? a small code-----------------

let uri = "ª¯°"; //0xAA, 0xAF, 0B0
let encoded = encodeURI(uri);
let decoded = decodeURI(encoded);

document.getElementById("demo").innerHTML = "Encoded URI:<br>" + encoded + "<br><br>" +
  "Decoded URI:<br>" + decoded;
<!DOCTYPE html>
<html>
<body>
  <h1>JavaScript Global Methods</h1>
  <h2>The decodeURI() Method</h2>

  <p id="demo"></p>
</body>
</html>

OUTPUT-------

    Encoded URI:
    %C2%AA%C2%AF%C2%B0

    Decoded URI:
ª¯°

as you can see some extra characters like 'C2' is generated how can I generate these strings without C2?

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

The encodeURI function is encoding your string to UTF-8

According to UTF-8 wikipedia page:

The first 128 characters (US-ASCII) need one byte. The next 1,920 characters need two bytes to encode.

Encoding individual characters from "ª¯°" will result in a similar pattern since they do not exist in the first 128 chars from US-ASCII table.

A similar answer also exists on this post: What is "=C2=A0" in MIME encoded, quoted-printable text?

console.log("ª " + encodeURI("ª"));
console.log("¯ " + encodeURI("¯"));
console.log("° " + encodeURI("°"));