Skip to content

Commit

Permalink
More encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
ghluka committed Apr 27, 2024
1 parent 8010871 commit eeb9c62
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion techdemos/encode/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ const codes = {
encode: text => btoa(text),
decode: text => atob(text)
},
"Base16 (Hexadecimal)": {
encode: text => text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("").toUpperCase(),
decode: text => text.split(/(\w\w)/g).filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("")
},
"Base2 (Binary)": {
encode: text => text.split("").map(c => c.charCodeAt(0).toString(2).padStart(8, "0")).join(" "),
decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 2))).join("")
},
"URL": {
encode: text => encodeURI(text),
decode: text => decodeURI(text)
},
"URL (component)": {
encode: text => encodeURIComponent(text),
decode: text => decodeURIComponent(text)
}
},
"URL (aggressive)": {
encode: text => "%" + text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase(),
decode: text => text.split("%").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("")
},
}
for (const code in codes) {
let element = document.createElement("option");
Expand Down

0 comments on commit eeb9c62

Please sign in to comment.