using System;
namespace SteamWare
{
///
/// classe con funzioni specifiche di calcolo
///
public class calcoli
{
///
/// inizializzazione classe
///
protected calcoli()
{
}
///
/// elenco dei caratteri base 36: 0..9A..Z
///
protected String baseCharList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
///
/// converte un intero in un numero a base 36 (0..9A..Z)
///
/// num intero da convertire
/// base, max 36 (0..9A..Z)
///
public String ConvertToBase(int num, int nBase)
{
// check if we can convert to another base
if (nBase < 2 || nBase > baseCharList.Length)
return "";
int r;
String newNumber = "";
// in r we have the offset of the char that was converted to the new base
while (num >= nBase)
{
r = num % nBase;
newNumber = baseCharList[r] + newNumber;
num = num / nBase;
}
// the last number to convert
newNumber = baseCharList[num] + newNumber;
return newNumber;
}
///
/// converte da base di dimensione nBase a valore intero
///
/// valore in formato nBase
/// base, max 36 (0..9A..Z)
///
public int ConvertFromBase(string valore, int nBase)
{
int answ = 0;
int moduloPos = 0;
int valorePos = 0;
if (nBase < 2 || nBase > baseCharList.Length)
return answ; // errore di base...
// percorro il mio vaore string e ri-compongo il numero INT...
for (int i = 0; i < valore.Length; i++)
{
// converto il valore in formato nBase in valore intero da moltiplcare x la base...
moduloPos = baseCharList.IndexOf(valore[valore.Length - i - 1]);
valorePos = Convert.ToInt32(Math.Pow(Convert.ToDouble(nBase), Convert.ToDouble(i)));
// leggo da sx con potenze crescenti della base
answ = answ + valorePos * moduloPos;
}
return answ;
}
///
/// metodo di accesso singleton
///
public static calcoli obj = new calcoli();
}
}