12,950,090 members (50,146 online)
Alternative Article
alternative version

Stats

5.7K views
4 bookmarked
Posted 24 Nov 2012

The Hexatridecimal Numbering System

, 24 Nov 2012 CPOL
 Rate this:
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" ;

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

Share

 Software Developer (Senior) 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

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

"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

You may also be interested in...

 Pro

 First Prev Next
 My vote of 3 Toli Cuturicu25-Nov-12 12:42 Toli Cuturicu 25-Nov-12 12:42
 Re: My vote of 3 PIEBALDconsult26-Dec-12 9:22 PIEBALDconsult 26-Dec-12 9:22
 return? Toli Cuturicu25-Nov-12 5:28 Toli Cuturicu 25-Nov-12 5:28
 Re: return? PIEBALDconsult25-Nov-12 6:27 PIEBALDconsult 25-Nov-12 6:27
 Re: return? Toli Cuturicu25-Nov-12 8:25 Toli Cuturicu 25-Nov-12 8:25
 Re: return? PIEBALDconsult25-Nov-12 8:36 PIEBALDconsult 25-Nov-12 8:36
 Last Visit: 31-Dec-99 18:00     Last Update: 25-May-17 13:40 Refresh 1