Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

The Hexatridecimal Numbering System

3.50/5 (2 votes)
24 Nov 2012CPOL1 min read 12K  
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)