Click here to Skip to main content
Licence CPOL
First Posted 9 Jul 2010
Views 6,922
Downloads 41
Bookmarked 7 times

The Hexatridecimal Numbering System

By | 9 Jul 2010 | Article
This article shows code for creating your own HexaTriDecimal type (Yes, I know that’s quite a mouthful, but read on to know more.)

Introduction

I recently needed to generate unique and incremental strings that could be used as unique ids for a project I have been working on for some time now. The string id (type) had to be short and capable of being incremented or decremented, and also comparable with both its own type as well as bare strings.

A good application of this might be something like the URLs you see in http://bit.ly. For example, my open source project site can be accessed using the url http://bit.ly/aJHhTb. This link will land you on the MnemonicFS main page.

Background

Basically, I wanted to have a numbering system that had 36 characters, as against the "standard" 10 chars we have in the decimal numbering system (‘0’ through ‘9’), and 16 characters in the hexadecimal system (‘0’ through ‘9’ followed by ‘A’ through ‘F’). Why not a numbering system with 36 characters (‘0’ through ‘9’, followed by ‘A’ through ‘Z’)? This I christened as the hexatridecimal (HTD) numbering system, only to learn later that that, in fact is the term used for describing base-36 numbers. Using this numbering scheme, with a place width of just 4, you can have almost up to 1.68 million different string combinations. Naturally, as you keep increasing the place width, you end up with an exponentially larger number of combinations.

Using the Code

In the code below (it’s C#/.NET), we have the class, HexaTriDecimalType (HTD), which embodies this numbering system, albeit to a very small degree. As we keep incrementing a HexaTriDecimalType variable, it runs from ‘0' through ‘9', then ‘A’ through ‘Z’, followed by 10, with the tens place getting incremented by one, and so forth. It also has functionalities for both incrementing and decrementing the HTD variable. Like I mentioned earlier, it does not have full-blown capabilities expected of a class of this sort since, among other things, it doesn’t support floating-point operations. And yes, the code is not terribly efficient.

When you create an object of type HexaTriDecimalType, the default value is ‘0000000000’ (with a default place width of 10). You can do increment and decrement operations, as also comparison of two objects of type HexaTriDecimalType, and a HexaTriDecimalType object with a bare string.

HexaTriDecimalType h1 = new HexaTriDecimalType ();
for (int i = 0; i < 100; ++i) {
    Console.WriteLine (i + " -> " + h1++);
} 

The HexaTriDecimalType has multiple constructors. The default constructor creates an object with a default place width of 10 chars. You can also specify your own value for this using the int constructor. There’s also a copy constructor, as well as constructors that take a bare string and convert it into a HexaTriDecimalType object. Of course, this string may only contain alpha-numeric chars, else the class will complain during object creation. There is also a static method that takes a string as its argument and returns the next value (incremented by one).

One thing to note is that the alphabetic chars accepted by the class are case-insensitive: I could have made it case sensitive so that each of the strings, “asdf”, “aSdF” and “ASDF”, is different from the other. This would have resulted in each place within the number yielding a total of 62 values instead of the 36 values that it has now. However, I did not do that; for one thing, if you think that HexaTriDecimal is quite a mouthful, try finding a name for that (smile). (BiSexagesimal, though technically correct, may not be the most appropriate one.)

The code also does not include functionality for converting between HexaTriDecimalType and decimal types, or for that matter any other “standard” type. I wish I could have done that, but I churned out this code in about two hours or so, and really, this suits my needs just fine, so I didn’t bother to go any further. And I only implemented two comparison operators (== and !=); the others, like <, <=, >, and >=, I did not, because it would have taken more time, and as you know that’s one commodity that’s always in short supply.

History

  • 9th July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Najeeb Shaikh

Technical Lead
Docutory Information Systems
India India

Member

Najeeb is based in the city of Bombay, India, and has been a part of the software industry for more than 11 years now. You can view his websites at the following urls:
Community Developer Wiki: CodeMinima.com: The Minimal Code Site

Open Source Project: MnemonicFS.org: The Mnemonic Filing System


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you PinmemberNajeeb Shaikh20:35 26 Jul '10  
GeneralPostcodes\zips Pinmemberreshi99922:20 12 Jul '10  
GeneralThis could be called Radix36 PinmemberMikeWilliams21:44 12 Jul '10  
GeneralMy vote of 3 PinmemberJohn Brett22:35 11 Jul '10  
GeneralI'll stick with base-64 PinmvpPIEBALDconsult5:26 9 Jul '10  
QuestionPotential changes Pinmemberludemade5:05 9 Jul '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 9 Jul 2010
Article Copyright 2010 by Najeeb Shaikh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid