2

This question already has an answer here:

I have a script which process some information coming from a web page. I guess that because of the encoding of the page, some special characters are encoded in hexadecimal. For example, I have the the string "%2f" that should be translated to "/".

How can I, in bash, translate those special characters in hex to ASCII? Any ideas?

marked as duplicate by chaos, Anthon, Archemar, dhag, Eric Renouf Oct 2 '15 at 14:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

Bash has a printf builtin, which can around the same as we could learn in C. The syntax a little bit differs.

printf '\x2f'

If you don't need to worry about higher-level data consistency problems, you can simply convert an url by this function:

function deUrl() {
    printf "${1//%/\\x}"
}

(It converts every % to a \x, then prints it with printf.)

4

Such entities can be decoded with this python one-liner:

$ python -c "import urllib, sys; print urllib.unquote(sys.argv[1])"  "%2f"
/

The code is not limited to single characters. It will accept more complex strings:

$ python -c "import urllib, sys; print urllib.unquote(sys.argv[1])"  "%2d and %2f"
- and /

Python's urllib.unquote is documented here.

  • Thanks! I will look at it as soon as I have time to play with python :) – Romain Pelissier Oct 7 '15 at 17:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.