0

I've been doing some work with url's and uri's and I have a question regarding a function I was using to encode/decode.

I've looked up online, and found a function to properly encode my code, using mostly String.fromCharCode() method. But the question is about the parameters passed.

I have something like this:

_utf8_encode : function (string) {
  string = string.replace(/\r\n/g,"\n");
  var utftext = "";
  for (var n = 0; n < string.length; n++) {
    var c = string.charCodeAt(n);
    if (c < 128) {
      utftext += String.fromCharCode(c);
    }
    else if((c > 127) && (c < 2048)) {
      utftext += String.fromCharCode((c >> 6) | 192);
      utftext += String.fromCharCode((c & 63) | 128);
    }
    else {
      utftext += String.fromCharCode((c >> 12) | 224);
      utftext += String.fromCharCode(((c >> 6) & 63) | 128);
      utftext += String.fromCharCode((c & 63) | 128);
    }
  }
  return utftext;
}

What bugs me is that operations inside the parenteses. I've done some tests with the c value to see if I got to any conclusion, but my efforts were in vain. Does anyone know what those operators (if I can call them that, '>>', '&', '|') mean?

Note that the function works, I just wanted to properly understand it.

Thanks very much! Bye!

Thiago Loddi
  • 1,759
  • 3
  • 17
  • 33
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators – elclanrs Jan 20 '15 at 23:29
  • you don't need to worry about what they do really. it's archaic and complicated math. it can be handy once in a blue moon, but it's not as important as it was decades ago. in short, it's ok to skip that part if you just want to build apps. – dandavis Jan 20 '15 at 23:33
  • I really wonder what you are using this function for (to which format does it encode?), and why/where you need this. – Bergi Jan 20 '15 at 23:59
  • I took a look at the documentation @elclanrs sent and I can see its really arhciac math indeed. Perhaps I should just ignore it all move on with my life. – Thiago Loddi Jan 21 '15 at 15:26
  • @Bergi I have to sent a uri in this format to request some info in a third-party server, so you should ask them what encoding is this, cause I'm lost here – Thiago Loddi Jan 21 '15 at 15:28

0 Answers0