• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjAz

Converting numbers to/from numeral systems

Let you convert integers to other numeral system, like a base36 string.

1
370 0 0 0 0 0

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);

Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

numeral-system

Mark 'numeral-system' tag as 'like'

Mark 'numeral-system' tag as 'ignore'


 @toha73 "Code Contributor"
716
July 15, 2010 9:06 PM
edited July 16, 2010 8:56 AM

Fork

 Converting numbers to/from numeral systems -  @toha73 Thursday 15, 2010 9:06 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010