Click here to Skip to main content
11,647,170 members (69,197 online)
Click here to Skip to main content

Tagged as

The Hexatridecimal Numbering System

, 24 Nov 2012 CPOL 4K 4
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "The Hexatridecimal Numbering System"

Introduction

Just a quick alternative to Najeeb Shaikh's article.

Background

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.

Base36

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
    Parse
    (
      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 )
        ,
          System.StringComparison.InvariantCultureIgnoreCase
        ) ;

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

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

      if ( sign )
      {
        temp *= -1 ;
      }

      return ( new Base36 ( temp ) ) ;
    }
    public override string
    ToString
    (
    )
    {
      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 ) ;

History

2012-11-24 First submitted

License

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

Share

About the Author

PIEBALDconsult
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 contrarian

---------------

"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

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



"We learn more from our mistakes than we do from getting it right the first time."

My first rule of debugging: "If you get a different error message, you're making progress."

My golden rule of database management: "Do not unto others' databases as you would not have done unto yours."

My general rule of software development: "Design should be top-down, but implementation should be bottom-up."

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
Toli Cuturicu25-Nov-12 12:42
memberToli Cuturicu25-Nov-12 12:42 
GeneralRe: My vote of 3 Pin
PIEBALDconsult26-Dec-12 9:22
memberPIEBALDconsult26-Dec-12 9:22 
Questionreturn? Pin
Toli Cuturicu25-Nov-12 5:28
memberToli Cuturicu25-Nov-12 5:28 
AnswerRe: return? Pin
PIEBALDconsult25-Nov-12 6:27
memberPIEBALDconsult25-Nov-12 6:27 
GeneralRe: return? Pin
Toli Cuturicu25-Nov-12 8:25
memberToli Cuturicu25-Nov-12 8:25 
GeneralRe: return? Pin
PIEBALDconsult25-Nov-12 8:36
memberPIEBALDconsult25-Nov-12 8:36 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150804.2 | Last Updated 24 Nov 2012
Article Copyright 2012 by PIEBALDconsult
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid