quinta-feira, 12 de julho de 2012

hex-to-ascii elisp function to fast decode protected donwload urls

Is really common to face download urls being "protected" but some annoying ringtone site. Some of then are so stupid that just encode the url as an hexadecimal string. Here is an example http://www.baixedetudo.net/id/?url=687474703a2f2f756c2e746f2f6f7977366b667732. Is easy to see the url here. I used perl to decode this, simple as in
print pack("H*", "687474703a2f2f756c2e746f2f6f7977366b667732"), "\n";
But as much I become an "emacs guy" more I do to easy my life.
Here is what I use from now to translate urls from hex to ascii The function usage is simple, just select the hex text and run it, you should get the translated text on clipboard.. You should be running emacs in its graphical form.
(defun hex-to-ascii (b e)
  "Translate the region from hex to ascii and copy it to clipboard.
I use that to translate urls in hex and paste it to url bar on my
browser."
  (interactive "r")
  (save-excursion
    (let ((i e)
           (x-select-enable-clipboard t)
           s)
      (while (> i b)
        (setq s (concat (format "%c" (read (concat "#x" (buffer-substring-no-properties (- i 2) i)))) s))
        (setq i (- i 2)))
      (kill-new s t)
      (message (format "%s copied to clip board" s)))))

I just keep this on my init.el.

Also I have done the opposite, a function that takes ascii string and returns its hex representation 
(defun ascii-to-hex (b e)
  "Translate an ascii string to a hex string and copy it to clipboard"
  (interactive "r")
  (save-excursion
    (let ((i b)
          (x-select-enable-clipboard t)
          s)
      (while (< i e)
        (setq s (concat s (format "%x" (get-byte i))))
        (setq i (+ i 1)))
      (kill-new s t)
      (message s))))

Nice and Easy :-)

Nenhum comentário:

Postar um comentário