简体   繁体   English

在c#中将base-27(或base-X)转换为base-10?

[英]Convert base-27 (or base-X) to base-10 in c#?

Is there a ready made function to be able to do base conversions in c#?是否有现成的函数可以在 C# 中进行基本转换? I am looking to convert from base 26 and base base 27 numbers to base 10. I can do it on paper but i am not a very experienced programmer and would rather not do it from scratch if possible.我希望从以 26 为基数和以 27 为基数的数字转换为以 10 为基数。我可以在纸上做到这一点,但我不是一个非常有经验的程序员,如果可能的话,我宁愿不要从头开始。 Thanks!谢谢!

There is a ready-made function to convert numbers from base 2, 8 or 16 to base 10 ( Convert.ToInt32 ).有一个现成的函数可以将数字从基数 2、8 或 16 转换为基数 10 ( Convert.ToInt32 )。 If you want to convert numbers from base 26 or base 27 to base 10, you'll have to do it yourself.如果要将数字从以 26 或以 27 为基数转换为以 10 为基数,则必须自己进行。

Now, I've never heard of base 26 numbers, so I'm just going to assume the 'digits' are A to Z (A having a value of 0, and Z having a decimal value of 25).现在,我从未听说过以 26 为基数的数字,所以我只是假设“数字”是 A 到 Z(A 的值为 0,Z 的十进制值为 25)。 To convert from base 26 to base 10 you should do the following:要将基数 26 转换为基数 10,您应该执行以下操作:

string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int GetDigitValue(char digit)
{
    return charset.IndexOf(digit);
}
int ConvertFromBase26(string number)
{
    int result = 0;
    foreach(char digit in number)
        result = result * charset.Length + GetDigitValue(digit);

    return result;
}

To convert from base 27, just add whatever character represents 26.要从基数 27 转换,只需添加代表 26 的任何字符。

Note: There's no error correction (you can convert the string "$#$@#$@" which will get you a nice negative number), and GetDigitValue is rather inefficient and should be replaced with a lookup table if you plan to do these conversions a lot.注意:没有错误更正(您可以转换字符串“$#$@#$@”,这将为您提供一个很好的负数),并且 GetDigitValue 效率相当低,如果您打算这样做,应该用查找表替换这些转换很多。

EDIT: A LINQ version, just for kicks.编辑:一个 LINQ 版本,只是为了好玩。

Again, no efficient lookup and no error correction, assuming the string consists only of legal digits.同样,假设字符串仅包含合法数字,则没有有效的查找和纠错。

string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int ConvertFromBase(string charset, string number)
{
    return number.Select(c=>charset.IndexOf(c)).Aggregate(0, (x, y) => x*charset.Length +y);
}

I think the first version is more readable, though.不过,我认为第一个版本更具可读性。

Building off of your answer.建立在你的答案之上。 You don't need a charset lookup list because you can just use the char ASCII Values.您不需要字符集查找列表,因为您可以只使用字符 ASCII 值。

int ConvertFromBase26(string number)
{
     return number.Select(digit => (int)digit - 64).Aggregate(0, (x, y) => x * 26 + y);
}

I used this to convert column string addresses to int in while programming with Excel.在使用 Excel 编程时,我使用它来将列字符串地址转换为 int in。

Assuming that you use base 26 using alphabet in one based (eg : A = 1, B = 2, ... , Z= 26, AA = 27, ... )假设您使用基于 26 的字母表(例如:A = 1, B = 2, ... , Z= 26, AA = 27, ...)

public int ToBase10(this string str)
{
    str = str.ToUpper();
    int number = 0;
    for (int index = 0; index < str.Length; index++)
        number += (str[index] - 'A' + 1) * (int)Math.Pow(26, str.Length - 1 - index);
    return number;
}

To call this function simply call like this要调用此函数,只需像这样调用

string PlateNumber = "TRX";
int number = PlateNumber.ToBase10();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM