1

I've been messing around with this for a while but I can't seem to get the right output basically I'm trying to make a js percentage encoder like the one that 2tap offers but URL encoding only encodes spaces. Here's what I've written so far:

const str = '4869937358'

let Hex = Buffer.from(str).toString('hex')
var i;
console.log(Hex)
var Ascii = ''
console.log(Hex.length)
for (i = 0; i < Hex.length; i++) {
    Ascii = Ascii + ' ' + Hex.charCodeAt(i)
}
console.log(Ascii)
var chars = Ascii.split(' ');
var Hexed = ''
for (i = 0; i < chars.length; i++) {
    Hexed =  Hexed + hex.textToHex(chars[i])
}
Hexed = '%' + Hexed
var Fin = Hexed.replace(/(..?)/g, '$1%').slice(0,-1)
console.log(Fin)

Now I think there might be a way easier way to do this but since I'm not that good at JS I believe I took the hard way.

Tyris
  • 41
  • 1
  • 1
  • 6
  • I'm not sure what your question is. Can you provide sample input and output? Is there a correctness issue or are you looking for a code review? Thanks for clarifying! – ggorlen Apr 16 '20 at 23:45
  • Remember that JS strings are built upon unicode (instead of ascii 8-bit charsets). – collapsar Apr 16 '20 at 23:46
  • Yes that might help but also I'm looking for a way to recreate [this](http://2tap.com/javascript-percent-encoder/) – Tyris Apr 16 '20 at 23:50

0 Answers0