Let you convert integers to other numeral system, like a base36 string.
public class NumeralSystem
{
public NumeralSystem(string literalString, bool caseSensitive)
: this(literalString.ToCharArray(), caseSensitive)
{
}
public NumeralSystem(char[] literals, bool caseSensitive)
{
this.IsCaseSensitive = caseSensitive;
if (literals.Length == 0)
throw new ArgumentException("A numeral system must contain some literals.", "literalString");
HashSet<string> set = new HashSet<string>(new string(literals).Select(c => c.ToString()));
if (set.Count != literals.Length)
throw new ArgumentException("Duplicate literals not allowed.", "literalString");
this.literals = literals;
if (!this.IsCaseSensitive)
{
this.literalsIgnoreCase = literals.Select(c => char.ToLower(c)).ToArray();
}
}
private char[] literals = new char[0];
private char[] literalsIgnoreCase = new char[0];
public bool IsCaseSensitive { get; private set; }
public string Encode(Int64 value)
{
if (value < 0)
throw new ArgumentException("Negative numbers not supported.", "value");
return this.Encode((UInt64)value);
}
public string Encode(UInt64 value)
{
string s = string.Empty;
while (value >= (UInt64)this.literals.Length)
{
UInt64 m = value % (UInt64)this.literals.LongLength;
s = this.literals[m] + s;
value = value / (UInt64)this.literals.Length;
}
s = this.literals[value] + s;
return s;
}
public string Encode(Int32 value)
{
if (value < 0)
throw new ArgumentException("Negative numbers not supported.", "value");
return this.Encode((UInt32)value);
}
public string Encode(UInt32 value)
{
string s = string.Empty;
while (value >= this.literals.Length)
{
var m = value % this.literals.Length;
s = this.literals[m] + s;
value = value / (UInt32)this.literals.Length;
}
s = this.literals[value] + s;
return s;
}
public string Encode(NumeralSystem fromSystem, string value)
{
return this.Encode(fromSystem.DecodeToUInt32(value));
}
public Int64 DecodeToUInt64(string value)
{
if (value == null)
throw new ArgumentNullException("value");
Int64 result = 0;
int i = 0;
foreach (char ch in value.Reverse())
{
int index;
if (this.IsCaseSensitive)
index = Array.IndexOf<char>(this.literals, ch);
else
index = Array.IndexOf<char>(this.literalsIgnoreCase, char.ToLowerInvariant(ch));
if (index == -1)
throw new ApplicationException(string.Format("Invalid character {0}. Valid characters are: {1}.", ch, new String(this.literals)));
result += index * (int)Math.Pow(this.literals.Length, i);
i++;
}
return result;
}
public UInt32 DecodeToUInt32(string value)
{
if (value == null)
throw new ArgumentNullException("value");
UInt32 result = 0;
int i = 0;
foreach (char ch in value.Reverse())
{
int index;
if (this.IsCaseSensitive)
index = Array.IndexOf<char>(this.literals, ch);
else
index = Array.IndexOf<char>(this.literalsIgnoreCase, char.ToLowerInvariant(ch));
if (index == -1)
throw new ApplicationException(string.Format("Invalid character {0}. Valid characters are: {1}.", ch, new String(this.literals)));
result += (uint)index * Convert.ToUInt32(Math.Pow(this.literals.Length, i));
i++;
}
return result;
}
public static NumeralSystem Hexadecimal
{
get
{
return new NumeralSystem("0123456789ABCDEF", false);
}
}
public static NumeralSystem Octal
{
get
{
return new NumeralSystem("01234567", false);
}
}
public static NumeralSystem Binary
{
get
{
return new NumeralSystem("01", false);
}
}
public static NumeralSystem Base36
{
get
{
return new NumeralSystem("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", false);
}
}
public static NumeralSystem Base62
{
get
{
return new NumeralSystem("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", true);
}
}
Example:
string myBase36String = NumeralSystem.Base36.Encode(81271);
// myBase36String will now contain: '1QPJ'
uint i = NumeralSystem.Base36.DecodeToUInt32(myBase36String);
Fork
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment


370
0




Mark 'c#' tag as 'like'
Mark 'c#' tag as 'ignore'