gpt4 book ai didi

c# - 在 C# 中从 Base36 解码为十进制

转载 作者:太空宇宙 更新时间:2023-11-03 18:41:29 25 4
gpt4 key购买 nike

public static Int64 Decode(string input)
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
pos++;
}
return result;
}

我正在使用一种方法将值从 base36 解码为十进制。该方法工作正常但是当我开始解码“000A”的输入值时,事情就开始出错了将其解码为 -1。

谁能看出哪里出了问题?我真的对代码及其工作方式感到困惑。

private const string CharList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

最佳答案

我只能假设您的 CharList 不包含 A,因此 IndexOf(c) 返回 -1 以显示未找到该字符。请记住 IndexOf 默认情况下区分大小写,因此如果您在 CharList 中使用小写字母而在 c 中使用大写字母,它不会匹配。

// pos = 0
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
// pos = 36^0 = 1
// CharList.IndexOf(c) gives -1 when not found
// therefore, it equates to:
result += -1 * 1

关于c# - 在 C# 中从 Base36 解码为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8355166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com