Base 58 encode (used for flic.kr short urls)


/ Published in: ActionScript 3
Save to your folder(s)

base 58 encoding in AS3 used to create a short url for flickr (flic.kr/p/) from a photo id. Function requires the flickr photo id to be passed in cast as a Number. Example below


Copy this code and paste it in your HTML
  1. /*
  2.  Function used to return flickr short url from a photo id.
  3.  example of usage
  4.  var shortFlickrURL:String = 'www.flic.kr/p/' + Base58Encoder.encode(Number('4725679319') );
  5. /// returns www.flic.kr/p/8cAkPD
  6. /// short url for this full url http://www.flickr.com/photos/ninjaparade/4725679319/
  7.  
  8.  
  9. */
  10.  
  11. package {
  12.  
  13. /**
  14. * @author ninjaparade
  15. */
  16. public class Base58Encoder {
  17.  
  18.  
  19. public static function encode( num : Number ) : String
  20. {
  21. var alphabet : String = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' ;
  22. var base_count : int = alphabet.length;
  23. var encode : String = "";
  24. while(num >= base_count) {
  25. var div : int = num / base_count;
  26. var mod : int = (num - base_count * Math.round(div) );
  27. encode = alphabet.charAt(mod) + encode;
  28. num = Math.round(div);
  29. }
  30.  
  31. if(num)
  32. {
  33. encode = alphabet.charAt(num) + encode;
  34. }
  35. return encode;
  36. }
  37. }
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.