DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Create Your Own bit.ly Using Base58

Create Your Own bit.ly Using Base58

Ignacio Coloma user avatar by
Ignacio Coloma
·
Mar. 18, 10 · Interview
Like (0)
Save
Tweet
Share
10.70K Views

Join the DZone community and get the full member experience.

Join For Free
Following the example of bit.ly, tinyurl, owl.ly, even coca-cola has joined the short URL bandwagon. Maybe it's because of Twitter, maybe the world is focusing on mobile devices or just saving bandwidth. In any case, it's not that hard to develop your own.

The implementation is quite straightforward: store the underlying URL in a database and return your primary key. This primary key when displayed as a number (Base10) is longer than strictly necessary; since each character can hold more than numbers, you are wasting space by constraining yourself to a decimal [0-9] range.

Enter Base58


Base58 is what you get after taking Base62 [a-zA-Z0-9] and removing any character that may induce to error when introduced by hand: 0 (zero), O (uppercase 'o'), I (uppercase 'i'), and l (lowercase 'L'). This concept was introduced to the general public by Flickr, which uses the following String:

123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ

To me, it makes more sense to use the natural order in the ASCII chart:

123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz

I will post here an example of the latest. For a Flickr implementation you can go here.

public class StringUtils {

private static final char[] BASE58_CHARS =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();

public static String numberToAlpha(long number) {
char[] buffer = new char[20];
int index = 0;
do {
buffer[index++] = BASE58_CHARS[(int) (number % BASE58_CHARS.length)];
number = number / BASE58_CHARS.length;
} while (number > 0);
return new String(buffer, 0, index);
}

public static long alphaToNumber(String text) {
char[] chars = text.toCharArray();
long result = 0;
long multiplier = 1;
for (int index = 0; index < chars.length; index++) {
char c = chars[index];
int digit;
if (c >= '1' && c <= '9') {
digit = c - '1';
} else if (c >= 'A' && c < 'I') {
digit = (c - 'A') + 9;
} else if (c > 'I' && c < 'O') {
digit = (c - 'J') + 17;
} else if (c > 'O' && c <= 'Z') {
digit = (c - 'P') + 22;
} else if (c >= 'a' && c < 'l') {
digit = (c - 'a') + 33;
} else if (c > 'l' && c <= 'z') {
digit = (c - 'l') + 43;
} else {
throw new IllegalArgumentException("Illegal character found: '" + c + "'");
}

result += digit * multiplier;
multiplier = multiplier * BASE58_CHARS.length;
}
return result;
}
}


An example of the expected output size:

44 = m
1431117682956369 = abc123ABC
// Long.MAX_VALUE
9223372036854775807 = CFq8pKn6mQN


This is hardly rocket science. You can find libraries to do this for ruby, javascript, PHP or even perl.

It's Base58 everywhere


Once you start doing this, it's nothing short of addictive. You can configure your web framework to use a Base58 converter for the key attributes in your URLs and start using '/shows/cr5W' instead of '/shows/234324323423'.

Everybody is doing this right now: Flickr, Facebook or YouTube (which seems to be using [0-9a-zA-Z_-]).

News - News - News


These days life is getting interesting by the minute. Last week our SimpleDS pet project got referenced by Google AppEngine Blog, and next April we are bringing the Javaspecialist Master Course to Madrid!

It's going to be awesome. This is the most advanced Java course I can refer to, and after some conversations with Dr. Kabutz he agreed to bring it to Madrid. It consists of four days of intense performance tuning, concurrency debugging, introspection, memory profiling and some neat, challenging Java puzzles. If you feel interested, you should consider to join us in April!

From http://icoloma.blogspot.com/

IT Relational database News Data Types mobile app Implementation Java (programming language) Concept (generic programming) Joins (concurrency library) Memory (storage engine)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Comparing Map.of() and New HashMap() in Java
  • What Is API-First?
  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Introduction to Spring Cloud Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: