我想将vb .net函数转换为c#..我已将其转换但未按预期返回输出
VB .net功能
Public Function DecryptString(ByVal EncryptedString As String) As String
Dim TempLine As String = Nothing
Dim TempChar As String = Nothing
Dim FinalStr As String = Nothing
Dim i As Integer = 0
Dim value As Integer = 0
FinalStr = Nothing
value = 0
If EncryptedString <> "" Then
For i = 1 To Len(EncryptedString)
Try
TempChar = Mid(EncryptedString, i, 1)
value = Asc(TempChar)
value = (value - (120))
FinalStr = FinalStr & Chr(value)
Catch
DecryptString = ""
Exit Function
End Try
Next
DecryptString = FinalStr
Exit Function
End If
DecryptString = ""
End Function
C#功能
public string DecryptString(string EncryptedString)
{
string substr = null;
char TempChar; int ExtraChars = 0; string ExtraOnes = "";
string FinalStr = null;
int i = 0;
int value = 0;
FinalStr = null;
value = 0;
if ((EncryptedString != ""))
{
for (i = 1; (i <= EncryptedString.Length); i++)
{
try
{
substr = EncryptedString.Substring((i - 1), 1);
TempChar = Convert.ToChar(substr);
value = (value - 120);
FinalStr = (FinalStr + ((char)(value)));
}
catch (System.Exception e)
{
// TODO: Exit Function: Warning!!! Need to return the value
return "";
}
}
return FinalStr;
}
return "";
}
我无法找到为什么它没有恢复正确的输出
答案 0 :(得分:2)
我冒昧地清理代码,因为这样更容易找到错误:
Public Function DecryptString(ByVal EncryptedString As String) As String
Dim FinalStr As New StringBuilder
Dim value As Integer = 0
If not String.IsNullOrEmpty(EncryptedString) Then
For Each c as Char in EncryptedString
Try
value = Asc(c)
value -= 120
FinalStr.Append(Chr(value))
Catch
Return ""
End Try
Next
End If
Return Finalstr.ToString
End Function
和C#版本一样:
public string DecryptString(string EncryptedString) {
StringBuilder FinalStr = new StringBuilder();
int value = 0;
if (!string.IsNullOrEmpty(EncryptedString)) {
foreach (char c in EncryptedString) {
try {
value = (int)c;
value -= 120;
FinalStr.Append(((char)(value)));
}
catch (System.Exception e) {
return "";
}
}
}
return Finalstr.ToString;
}
看看它是否适合你。
答案 1 :(得分:2)
试试这个:
<强> VB.Net to C# Conversion 强>
public string DecryptString(string EncryptedString)
{
string functionReturnValue = null;
string TempLine = null;
string TempChar = null;
string FinalStr = null;
int i = 0;
int value = 0;
FinalStr = null;
value = 0;
if (!string.IsNullOrEmpty(EncryptedString)) {
for (i = 1; i <= Strings.Len(EncryptedString); i++) {
try {
TempChar = Strings.Mid(EncryptedString, i, 1);
value = Strings.Asc(TempChar);
value = (value - (120));
FinalStr = FinalStr + Strings.Chr(value);
} catch {
functionReturnValue = "";
return functionReturnValue;
}
}
functionReturnValue = FinalStr;
return functionReturnValue;
}
functionReturnValue = "";
return functionReturnValue;
}
<强>参见:强>
1。) Strings.Asc Method (String) :返回表示与字符对应的字符代码的Integer值。 (命名空间:Microsoft.VisualBasic)
2。) What's the equivalent of VB's Asc() and Chr() functions in C#? :
对于Asc(),您可以将char转换为这样的int:
int i = (int)your_char;
对于Chr(),您可以从这样的int中转换回char:
char c = (char)your_int;
答案 2 :(得分:1)
问题是你输了(从不使用)TempChar
。
如果您只是进行此更改,那么您就回家了:
value = (TempChar - 120);
答案 3 :(得分:0)
你永远不会将角色的值分配给value
,所以你最终会得到-120,-240等等。
这是另一种方法,没有所有字符串操作:
if (!String.IsNullOrEmpty(EncryptedString))
{
for (i = 0; i < EncryptedString.Length; i++)
{
try
{
TempChar = EncryptedString[i];
value = (int)TempChar - 120;
FinalStr = FinalStr + (char)(value);
}
catch (System.Exception e)
{
// TODO: Exit Function: Warning!!! Need to return the value
return "";
}
}