Base 58 encode – flic.kr short urls

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

/*
 Function used to return flickr short url from a photo id.
 example of usage
 var shortFlickrURL:String = 'www.flic.kr/p/' + Base58Encoder.encode(Number('4725679319') );
/// returns www.flic.kr/p/8cAkPD
/// short url for this full url http://www.flickr.com/photos/ninjaparade/4725679319/


*/

package {

	/**
	 * @author ninjaparade
	 */
	public class Base58Encoder {

		
		public static function encode( num : Number ) : String
		{
		var alphabet : String = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' ;
			var base_count : int = alphabet.length;
			var encode : String = "";
			while(num >= base_count) {
				var div : int = num / base_count;
				var mod : int = (num - base_count * Math.round(div) );
				encode = alphabet.charAt(mod) + encode;
				num = Math.round(div);
			}

			if(num) 
			{
				encode = alphabet.charAt(num) + encode;
			}
			return encode;
		}
	}
}

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s