我发现了将C#代码片段转换为VB.NET的奇怪情况,代码是一个从基本10转换为基本36的小类,反之亦然。C#中的剩余运算符与Visual Basic中的MOD执行不同
关键的一点是这样的功能:
/// <summary>
/// A Base36 De- and Encoder
/// </summary>
public static class Base36
{
private const string CharList = "abcdefghijklmnopqrstuvwxyz";
/// <summary>
/// Encode the given number into a Base36 string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static String Encode(long input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray());
}
在VB.NET转换应导致:
Public NotInheritable Class Base36
Private Sub New()
End Sub
Private Const CharList As String = "abcdefghijklmnopqrstuvwxyz"
''' <summary>
''' Encode the given number into a Base36 string
''' </summary>
''' <param name="input"></param>
''' <returns></returns>
Public Shared Function Encode(input As Int64) As String
If input < 0 Then
Throw New ArgumentOutOfRangeException("input", input, "input cannot be negative")
End If
Dim clistarr As Char() = CharList.ToCharArray()
Dim result = New Stack(Of Char)()
While input <> 0
result.Push(clistarr(input Mod 36))
input /= 36
End While
Return New String(result.ToArray())
End Function
的问题是,在VB.NET模运算符执行不同的是实际上,如果你在C#中调用编码方法:
long l = 13072113072399;
string result = Base36.Encode(l); //Result is : 4mt8um0b3
同时呼吁在C#中的方法:
Dim l As Int64 = 13072113072399
Dim result As String = Base36.Encode(l) //Result is : 5nujsu3ar
该负责的差异是不同的结果,在某些情况下,模运算回报,为什么呢?
VB.NET中%余数运算符的等效值是多少?
如果您认为运营商是给你不同的结果,然后写了一个演示此小自足的例子。 **如何**完全不同的结果?我们不需要关心所有Base36问题。 –
VB.net是否使用基于1的数组或类似的东西? –
@JonathonReinhart:Ew编号:D – Ryan