Posted By

ninjaparade on 08/18/10


Tagged

flickr encode as3 base58


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

shalomfriss
tspitzr


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


 / Published in: ActionScript 3
 

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

  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  

You need to login to post a comment.