我需要将以下函数转换为 VB.NET,但我不确定如何处理该语句
res = (uint)((h * 0x21) + c);
完整功能:
private static uint convert(string input)
{
uint res = 0;
foreach (int c in input)
res = (uint)((res * 0x21) + c);
return res;
}
我创建了以下内容,但出现溢出错误:
Private Shared Function convert(ByVal input As String) As UInteger
Dim res As UInteger = 0
For Each c In input
res = CUInt((res * &H21) + Asc(c)) ' also tried AscW
Next
Return res
End Function
我错过了什么?有人可以解释一下细节吗?
请您参考如下方法:
您的代码是正确的。计算在几个字符后溢出,因为 res 在每次迭代中呈指数增长(导致溢出的不是字符的转换,而是溢出的无符号整数)。
默认情况下,C# 允许整数运算溢出 – VB 不允许。不过,您可以在 VB 的项目设置中禁用溢出检查。但是我会尽量不依赖这个。是否有必须移植此特定 C# 的原因?毕竟,您可以毫不费力地混合使用 C# 和 VB 库。
