我發現了將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