using System;
using System.Linq;
namespace Masuit.Tools.Excel;
///
/// 数制格式化器
///
internal class NumberFormater
{
///
/// 数制表示字符集
///
internal string Characters { get; set; }
///
/// 进制长度
///
public int Length => Characters.Length;
///
/// 起始值偏移
///
private readonly int _offset;
///
/// 数制格式化器
///
public NumberFormater()
{
Characters = "0123456789";
}
///
/// 数制格式化器
///
/// 符号集
/// 起始值偏移
public NumberFormater(string characters, int offset = 0)
{
if (string.IsNullOrEmpty(characters))
{
throw new ArgumentException("符号集不能为空");
}
Characters = characters;
_offset = offset;
}
///
/// 指定字符串转换为指定进制的数字形式
///
///
///
public long FromString(string str)
{
int j = 0;
return new string(str.ToCharArray().Reverse().ToArray()).Where(ch => Characters.Contains(ch)).Sum(ch => (Characters.IndexOf(ch) + _offset) * (long)Math.Pow(Length, j++));
}
}