Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Tip/Trick

On What Base Do You Stand?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
15 Mar 2016CPOL2 min read 6.5K   3   5
Convert numbers of any base to base10...

Introduction

There are times you have to work with some numbers not from the usual bases (like base10, base2, base16). The easiest way to handle them is to convert to base10, but .NET has no built in solution for that...

I will provide one...

Background

The trigger for this small tip was a QA question, that was solved by different poeple in different ways - none of them statisfied me :-)...

Using the Code

C#
static decimal ToBase10(string Value, string BaseChars)
{
  int nBase = BaseChars.Length;
  decimal nResult = 0;
  int nPos = Value.Length - 1;

  foreach (char chDigit in Value.ToUpper().ToArray())
  {
    nResult += (decimal)Math.Pow(nBase, nPos) * BaseChars.ToUpper().IndexOf(chDigit);

    nPos--;
  }

  return (nResult);
}

This code can be used anywhere in your code, in the most simplest way - call it...

C#
decimal base10 = ToBase10("YYY", "XYZ"); // the result is 13, and I will explain it promptly...

Samples and Explanation

Let's start with the parameters of the method...

  • Value - it is the basen value to be converted to base10
  • BaseChars - a string containing the 'digits' - in order - used in the specific base
Note: For a single digit you can use only a single sign, but any sign!

Now let's see some samples...

Base3 in Different Way...

C#
ToBase10("YYY", "XYZ");

This is a base3 number where the 'digits' to use are X, Y, Z (instead of 0, 1, 2 as we would do normally)... So, following the rules of math, the value of this number is:

$3^{0} * Y + 3^{1} * Y + 3^{2} * Y = Y + 3 * Y + 9 * Y = 13 * Y$

Y is the second 'digit' in this sample, so it's true value is 1... So the final resutl is 13!

Base36 - English numerics...

C#
ToBase10("ENGLISH", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");

The base is now 36 (26 letters and 10 digits), so the computation goes like this:

$36^{0} * H + 36^{1} * S + 36^{2} * I + 36^{3} * L + 36^{4} * G + 36^{5} * N + 36^{6} * E = $

$1 * H + 36 * S + 1296 * I + 46656 * L + 1679616 * G + 60466176 * N + 2176782336 * E$

The values of the 'digits' according their position are:

$E = 14, N = 23, G = 16, L = 21, I = 18, S = 28, H = 17$

Inserting these into the equation, you will have 31,893,552,737 as result - a nice number...

Base10 - Can You Recognize?

C#
ToBase10("(!", "!@#$%^&*()");

It is 80, but I will not explain why...that's for you...

License

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


Written By
Software Developer (Senior)
Israel Israel
Born in Hungary, got my first computer at age 12 (C64 with tape and joystick). Also got a book with it about 6502 assembly, that on its back has a motto, said 'Try yourself!'. I believe this is my beginning...

Started to learn - formally - in connection to mathematics an physics, by writing basic and assembly programs demoing theorems and experiments.

After moving to Israel learned two years in college and got a software engineering degree, I still have somewhere...

Since 1997 I do development for living. I used 286 assembly, COBOL, C/C++, Magic, Pascal, Visual Basic, C#, JavaScript, HTML, CSS, PHP, ASP, ASP.NET, C# and some more buzzes.

Since 2005 I have to find spare time after kids go bed, which means can't sleep to much, but much happier this way...

Free tools I've created for you...



Comments and Discussions

 
QuestionThat is neat Pin
Kenneth Haugland6-Nov-17 6:57
mvaKenneth Haugland6-Nov-17 6:57 
AnswerRe: That is neat Pin
Kornfeld Eliyahu Peter6-Nov-17 8:17
professionalKornfeld Eliyahu Peter6-Nov-17 8:17 
GeneralRe: That is neat Pin
Kenneth Haugland6-Nov-17 9:18
mvaKenneth Haugland6-Nov-17 9:18 
PraiseRe: That is neat Pin
Kornfeld Eliyahu Peter6-Nov-17 9:37
professionalKornfeld Eliyahu Peter6-Nov-17 9:37 
GeneralRe: That is neat Pin
Kornfeld Eliyahu Peter6-Nov-17 9:39
professionalKornfeld Eliyahu Peter6-Nov-17 9:39 

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.