65.9K
CodeProject 正在变化。 阅读更多。
Home

Base N 转换器 (N = 10-62)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.37/5 (14投票s)

2007年11月23日

CPOL
viewsIcon

85049

downloadIcon

333

可以将基数 10 转换为基数 62。

Screenshot - figure1.jpg

Screenshot - figure2.jpg

引言

这个类使用了十六进制算法的思想并对其进行了扩展。或者,更像是 Base64 转换。

public static class ExpandableHexConverter
{
    public enum ExpandLevel
    {
        A = 11,
        B,
        C,
        D,
        E,
        F,
        G,
        H,
        I,
        J,
        K,
        L,
        M,
        N,
        O,
        P,
        Q,
        R,
        S,
        T,
        U,
        V,
        W,
        X,
        Y,
        Z,
        UseCaseSensitive = 62
    }

    public static string ToHex(long value, ExpandLevel ExpandBy)
    {
        return loopRemainder(value, (long)ExpandBy);
    }

    public static long ToInt64(string value, ExpandLevel ExpandBy)
    {
        value = validate(value, ExpandBy);
        long returnvalue = 0;
        for (int i = 0; i < value.Length; i++)
            returnvalue += (long)Math.Pow((long)ExpandBy, 
                           (value.Length - (i + 1))) * CharToVal(value[i]);
        return returnvalue;
    }

    private static string loopRemainder(long value, long PowerOf)
    {
        long x = 0;
        long y = Math.DivRem(value, PowerOf, out x);
        if (y > 0)
            return loopRemainder(y, PowerOf) + ValToChar(x).ToString();
        else
            return ValToChar(x).ToString();
    }
    private static char ValToChar(long value)
    {
        if (value > 9)
        {
            int ascii = (65 + ((int)value - 10));
            if (ascii > 90)
                ascii += 6;
            return (char)ascii;
        }
        else
            return value.ToString()[0];
    }
    private static long CharToVal(char value)
    {
        long chrval = (int)value;
        if (chrval > 96)
            return (chrval - 71) + 10;
        else if (chrval > 64)
            return (chrval - 65) + 10;
        else
            return int.Parse(value.ToString());
    }
    private static string validate(string input, ExpandLevel ExpandBy)
    {
        string validchars = "";
        string rtnval = "";
        for (long c = 0; c < (long)ExpandBy; c++)
            validchars += ValToChar(c);
        foreach (char i in input)
            if (validchars.Contains(i.ToString()))
                rtnval += i;
        return rtnval;
    }
}

使用代码

正常的十六进制看起来是这样

string HexValue = ExpandableHexConverter.ToHex(255, 
                     ExpandableHexConverter.ExpandLevel.F);

结果

HexValue = FF

扩展的十六进制可能看起来是这样

//NOTE: Z Level selected and the value to convert is larger.
string HexValue = ExpandableHexConverter.ToHex(1295, 
                        ExpandableHexConverter.ExpandLevel.Z);

结果

Result:
HexValue = ZZ

扩展的十六进制也可能看起来是这样

//NOTE: UseCaseSensitive Level selected and the value 
//to convert is larger that the previus example.
string HexValue = ExpandableHexConverter.ToHex(3843, 
                      ExpandableHexConverter.ExpandLevel.UseCaseSensitive);

结果

Result:
HexValue = zz

一些比较

正常的十六进制算法是基数 16,这里进行了演示

  • F = 15 (0 到 15 = 16)
  • FF = 255
  • FFF = 4095

扩展的十六进制算法设置为 Z基数 36,这里进行了演示

  • Z = 35 (0 到 35 = 36)
  • ZZ = 1295
  • ZZZ = 46655 (11 倍于正常的十六进制)

扩展的十六进制算法设置为区分大小写基数 62,这里进行了演示

  • z = 61 (0 到 61 = 62)
  • zz = 3843
  • zz = 238327(58 倍于正常的十六进制)

历史

  • 2007年11月23日 - 发布了第一个版本。
  • 2007年11月23日 - 文章重命名...
© . All rights reserved.