Skip to content

Instantly share code, notes, and snippets.

@carlosbrando
Created May 27, 2011 19:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save carlosbrando/995982 to your computer and use it in GitHub Desktop.
Save carlosbrando/995982 to your computer and use it in GitHub Desktop.
Erlang Base64
-module (base64ex).
-export ([urlsafe_decode64/1, urlsafe_encode64/1]).
urlsafe_decode64(Str) ->
Str2 = re:replace(Str, "-", "+", [global, {return,list}]),
Str3 = re:replace(Str2, "_", "/", [global, {return,list}]),
base64:decode(Str3).
urlsafe_encode64(Bin) ->
Bin2 = base64:encode(Bin),
Bin3 = re:replace(binary_to_list(Bin2), "\\+", "-", [global, {return,list}]),
re:replace(Bin3, "/", "_", [global, {return,list}]).
@tuafeeqahmed
Copy link

Can you write "money denomination" code in erlang Language ?

@adolfont
Copy link

adolfont commented Sep 1, 2020

The gist above was mentioned by me here:
Why did José Valim create Elixir?
https://medium.com/@adolfont/why-did-jos%C3%A9-valim-create-elixir-a6e210b7f2a1

@adolfont
Copy link

adolfont commented Sep 1, 2020

How to test the code above:

Save the file as base64ex.erl

Open Erlang Shell (erl) and:

> c("base64ex.erl").

> base64ex:urlsafe_decode64("T2zDoSwgbXVuZG8h").

> base64ex:urlsafe_encode64(<<"Olá, mundo!"/utf8>>).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment