5,445,109 members and growing! (14,902 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Base 36 type for .NET (C#)

By Steve Barker 333

A struct that provides base-36 functionality.
C++, C#, .NET, NT4, Win2K, WinXP, Win2003, WindowsVisual Studio, VS.NET2003, Dev

Posted: 7 Jun 2005
Updated: 7 Jun 2005
Views: 34,354
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 3.61 Rating: 2.65 out of 5
13 votes, 56.5%
1
2 votes, 8.7%
2
1 vote, 4.3%
3
4 votes, 17.4%
4
3 votes, 13.0%
5

Introduction

I found myself wanting to express a whole host of numbers in as few characters as possible. I came up with the idea of using a Base36 type to represent Base 36 numbers. This scheme is basically an extension to hexadecimal, but whereas hexadecimal stops at 15 (F), Base 36 carries on, with G being 16, all the way up to Z, which is 35. 10 in Base 36 is in fact 36 in Base 10. Base 36 has the benefit that the "numbers" are expressed by characters that are readable to humans, so this can be a good way of passing numerical data over the telephone for instance. Using Base 36, numbers up to 46,655 can be expressed using only 3 characters (ZZZ).

The Code

The code for my Base36 struct is extremely simple, so I won't go into details explaining it; download it and take a look. I've overloaded as many operators as I could, so Base 36 numbers can be added, subtracted, multiplied etc... I've tried to keep the methods of my struct consistent with the way that Microsoft labels type methods. You can instantiate a Base 36 "number" in string format, or from a standard Base 10 number:

Base36 b1 = 104;
//This has the value 104 in base 10.


Base36 b2 = "DSGFDFDZ434";
//This has the value 50,420,080,957,151,344 in Base 10.

The source files should be made into a class library. The demo project should be made into a console application, with a reference added to the class library; this demonstrates my struct in action!

And that's it! The struct seems to work very well, although I'm sure some of the code can be optimised. Good luck!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Steve Barker 333


I left Nottingham University in 2000 with an MSci in Mathematical Physics proudly in my grasp... but not a clue what I wanted to do with it!

Somewhere along the way however, I managed to swap Quantum Mechanics and General Relativity for RecordSets and Arrays, and took on some freelance VB development work. I soon realised I'd found my vocation, and promptly found an IT post in Nottingham, where I began developing Access databases (using VBA) for local government offices.

At some point I moved from Access to SQL Server, and before long, got my grubby palms on a copy of Visual Studio .NET. A very important milestone in my development career I think! Armed with my new found .NET knowledge, and with four years IT experience under my belt, I found my second job as Project Manager/IT Systems Developer, which brings us nicely up to date. I now work almost exclusively in .NET, writing mainly C# Windows applications and the odd web service, although I’m working on my ASP.NET too: the future of software development.

Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralI can't find the Exception Classes in the source codememberjames.wren6:54 30 Jun '08  
GeneralRe: I can't find the Exception Classes in the source codememberSteve Barker 33313:17 30 Jun '08  
GeneralRe: I can't find the Exception Classes in the source codememberSteve Barker 33313:26 30 Jun '08  
GeneralTesting Code for Base36ToUInt64()memberRussell Mangel23:00 23 Mar '06  
GeneralTesting Code for UInt64ToBase36()memberRussell Mangel22:55 23 Mar '06  
GeneralHere is my version of Base36memberRussell Mangel22:53 23 Mar '06  
Generalwhat about this...memberJeremy Falcon10:46 8 Jun '05  
GeneralRe: what about this...memberSteve Barker 3335:30 12 Jun '05  
GeneralRe: what about this...memberJeremy Falcon16:35 12 Jun '05  
GeneralFantastic!memberGeminiMan18:41 7 Jun '05  
GeneralRe: Fantastic!membertupacs015:50 8 Jun '05  
GeneralRe: Fantastic!memberumuhk9:47 8 Jun '05  
GeneralRe: Fantastic!memberKeith Farmer9:55 8 Jun '05