{{ This is a re-published article: it first appeared on CodeProject back in 2010. Unfortunately they unceremoniously substituted my article with another author’s, assigning no explanation whatsoever. }}
I recently needed to generate unique and incremental strings that could be used as unique ids for a project that I have been working on for some time now. I need to have a unique string ids for objects as they get created, and they have to be short and capable of being incremented or decremented, and also comparable with both their 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’s wiki on GitHub can be accessed using the the url http://bit.ly/2z9Y7IV.
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.
PS: You will find the code for this here: https://www.wikiofcode.com/The_HexaTriDecimal_Numbering_System
In the C# code — find it in its entirety on my code wiki here) –, 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, this does not have full-blown capabilities expected of a class of this sort since, among other things, it doesn’t support floating-point type operations. And yes, the code is not terribly efficient yet.
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);
++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.
Again the code does not include functionality for converting between HexaTriDecimalType and decimal types, or for that matter any other primitive 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 going 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.
Please do go through the code listing on my wiki: https://www.wikiofcode.com/The_HexaTriDecimal_Numbering_System.