Click here to Skip to main content
15,166,498 members
Articles / Programming Languages / C#
Posted 24 Nov 2012

Tagged as


4 bookmarked

The Hexatridecimal Numbering System

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
24 Nov 2012CPOL1 min read
This is an alternative for "The Hexatridecimal Numbering System"


Just a quick alternative to Najeeb Shaikh's article.


It occurs to me (with giving his code only a quick look) that Najeeb really only needs a way to Parse and ToString an integer value in base-36 and that all other functionality can be provided by a built-in type. Such functionality can be provided simply by a couple of static methods, but here I'll provide a simple class that wraps an integer (long) and provides appropriate Parse and ToString methods. One thing I didn't include is the padding out to ten characters, but I expect that's easy to add if you want.


Ideally I would make it generic so you can specify the underlying type, but so far .net doesn't support that very well. The class has two fields; one to hold the value of the instance and one constant to hold the digits to use. There is one constructor to set the Value field.

namespace PIEBALD.Types
  public sealed class Base36
    private const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

    private readonly long Value ;

    public Base36
      long Value
      this.Value = Value ;

      return ;

By including implicit operators to and from long we automatically gain all the functionality of long.

public static implicit operator long
  Base36 Value
  return ( Value.Value ) ;

public static implicit operator Base36
  long Value
  return ( new Base36 ( Value ) ) ;

All that's left are the Parse and ToString methods; I considered using ones I wrote a few years back that can do any base from 2 to 64, but I decided to write fresh ones instead.

public static Base36
  string Value
  if ( System.String.IsNullOrEmpty ( Value ) )
    throw ( new System.ArgumentException ( "Value must not be null or empty" , "Value" ) ) ;

  long temp = 0 ;
  bool sign = Value [ 0 ] == '-' ;

  for ( int i = sign?1:0 ; i < Value.Length ; i++ )
    int o = digits.IndexOf
      Value.Substring ( i , 1 )
    ) ;

    if ( o == -1 )
      throw ( new System.Exception ( System.String.Format
        "Invalid character {0} at postion {1}"
        Value [ i ]
      ) ) ) ;

    temp *= digits.Length ;
    temp += o ;

  if ( sign )
    temp *= -1 ;

  return ( new Base36 ( temp ) ) ;
public override string
  string result = "0" ;

  if ( this.Value != 0 )
    long temp = this.Value ;
    bool sign = temp < 0 ;

    if ( sign )
      temp *= -1 ;

    System.Text.StringBuilder sb = new System.Text.StringBuilder() ;

    while ( temp > 0 )
      long o = temp % digits.Length ;

      sb.Insert ( 0 , digits [ (int) o ] ) ;

      temp /= digits.Length ;

    if ( sign )
      sb.Insert ( 0 , '-' ) ;

    result = sb.ToString() ;

  return ( result ) ;

And that's it. Note that performance of mine is biased toward mathematical operations (and memory footprint) while his is biased toward getting the string representation, so that may affect your choice of which to use.

Usage is straight-forward; it behaves like a long with overridden Parse and ToString methods. (Oh how I wish I could just do that!)

PIEBALD.Types.Base36 m = 42 ;
PIEBALD.Types.Base36 n = PIEBALD.Types.Base36.Parse ( "42" ) ;
m++ ;
n-- ;
PIEBALD.Types.Base36 o = m * n ;
System.Console.WriteLine ( "The value is {0}" , o ) ;

// Unfortunately this doesn't work as expected because the value gets cast to a long 
// before ToString is called 
System.Console.WriteLine ( o ) ;


2012-11-24 First submitted


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


About the Author

Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB, acknowledged pedant and contrarian


"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]

"Typing is no substitute for thinking." -- R.W. Hamming

"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon

"linq'ish" sounds like "inept" in German -- Andreas Gieriet

"Things would be different if I ran the zoo." -- Dr. Seuss

"Wrong is evil, and it must be defeated." –- Jeff Ello

"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw

“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

"Omit needless local variables." -- Strunk... had he taught programming

Comments and Discussions

QuestionI was the one who originally wrote this article Pin
Najeeb Shaikh20-Apr-18 22:50
MemberNajeeb Shaikh20-Apr-18 22:50 
GeneralMy vote of 3 Pin
Toli Cuturicu25-Nov-12 13:42
MemberToli Cuturicu25-Nov-12 13:42 
GeneralRe: My vote of 3 Pin
PIEBALDconsult26-Dec-12 10:22
professionalPIEBALDconsult26-Dec-12 10:22 
Toli Cuturicu wrote:
Almost al rules are broken

What rules would those be? Confused | :confused:

I believe I follow all my rules. If you have your own rules you may go ahead and follow them; I won't think any less of you. But there is absolutely no reason for me to follow yours or for you to follow mine.
Questionreturn? Pin
Toli Cuturicu25-Nov-12 6:28
MemberToli Cuturicu25-Nov-12 6:28 
AnswerRe: return? Pin
PIEBALDconsult25-Nov-12 7:27
professionalPIEBALDconsult25-Nov-12 7:27 
GeneralRe: return? Pin
Toli Cuturicu25-Nov-12 9:25
MemberToli Cuturicu25-Nov-12 9:25 
GeneralRe: return? Pin
PIEBALDconsult25-Nov-12 9:36
professionalPIEBALDconsult25-Nov-12 9:36 

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

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