Monday, 24 September 2012

Not what I was intending to do today

I'm back in a cold, wet London after a rather sunny (if quite cool) weekend in Ireland.  And I have a day off work today to recharge my batteries a bit after all the planes, trains & automobiles in the last coupla days.

I was going to write an article "not really about closures" today, but I got sidetracked whilst looking at StackOverflow, and ended up doing something else instead.  It's just a function to convert a number from one arbitrary base to another arbitrary base, and I pinched the algorithm from someone else, but I quite like it so I'm gonna share it here.  And I'll submit it to CFLib too, as  - surprisingly - they do not already seem to have a function to do this.  Weird: I thought everyone has written one of these at some point or another (I know it's about the third time I have written a variation on this theme).

The original StackOverflow question was this one: someone wanting a unique ID but not one that was as long as a UUID.  I was going to suggest they converted the UUID to base-62 (0-9a-zA-Z, so quite human-friendly) which would be substantially shorter.  But before doing this, I needed to find the function to do it (predicting the person's first follow-up question), and I wanted to see how long a base-62 version of a UUID would be first (it's actually still pretty long).

Having drawn a blank looking for a ColdFusion-based answer, I did a general google and came up with this site which does the trick, albeit in JavaScript.  It's quite handy though.

Anyway, and apropos of not much given the bod on StackOverflow went a different way with his solution, here's a CF10 rendition of the JS function.  It's CF10-only (well: and Railo 4.x) because it has an inner function, which won't work on other  flavours / versions of CF.  It's be easy enough to defactor the inner function though, if you wanted to get this working on older platforms.

And here's some testing / proof:

And the results of said proof:
DESCRIPTIONA fairly easy to visually-test example
DESCRIPTIONUsing a bespoke 'alphabet' (OCTAL in this case)
DESCRIPTIONAnother one using a bespoke 'alphabet' (this is URL safe)
DESCRIPTIONConverting to/from the same base
DESCRIPTIONMulti-step proof it all works correctly: 1) from a GUID to BASE62
DESCRIPTIONMulti-step proof it all works correctly: 2) from BASE62 to BINARY
DESCRIPTIONMulti-step proof it all works correctly: 3) from BINARY back to the original HEX

I welcome any commentary or suggested improvements.  Indeed it's on github, so you can DIY if you like (I have no idea how github really works, I just commit my scratch code there for safe-keeping, but you can see the code there and [do stuff with it, I guess]).

Once it's up on CFLib, I'll post a link to it here too...  Done! (this is one of the benefits of being a CFLib approver... Ray will let me approve my own stuff :-)

Now: off to the forest I go (the rain has stopped, so shall get a dose of the outdoors whilst I can), but I'll come back later and not discuss closures, as was my original plan today...