1

I want to convert a vb .net function to c#..i have converted it but it is not returning the output as expected

VB .net Function

 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# function

 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 "";
    }

I am not able to find why it is not returing the right output

Annie
  • 670
  • 1
  • 7
  • 20
  • It looks like you're missing a step after `TempChar = Convert.ToChar(substr);` - you need to assign the ASCII value of the character. Try this: `value = (int)TempChar;`. – Tim Oct 13 '12 at 06:17
  • thnx for helping me out friends – Annie Oct 13 '12 at 07:11

4 Answers4

2

I took the liberty to clean up the code a little as it's easier to locate errors this way:

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

And as C# version:

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;
}

See if that works for you.

  • your code is also not working. The resulted string is showing ]̤ɤ character in place of = sign. Thnx anyway – Annie Oct 13 '12 at 06:42
  • I don't know what you are decoding, I'm just using your formula to do it :-) –  Oct 13 '12 at 06:46
  • @shalini, but that being said, it looks like your input-string is UNICODE and not straight ASCII. If you could show an example how the string gets encoded, in what format as well as an example of a raw string and encoded string, we might be able to help you a step further. –  Oct 13 '12 at 07:05
  • Hi Abdias! While encrypting 120 is added and while decrypting 120 is subtracted. The code is same – Annie Oct 18 '12 at 07:11
2

Try this:

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;
}

Refer:

1.) Strings.Asc Method (String) :Returns an Integer value representing the character code corresponding to a character. (Namespace: Microsoft.VisualBasic)

2.) What's the equivalent of VB's Asc() and Chr() functions in C#?:

For Asc() you can cast the char to an int like this:

int i = (int)your_char;

and for Chr() you can cast back to a char from an int like this:

char c = (char)your_int;
Community
  • 1
  • 1
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
  • OP will need to add a reference to the Microsoft.VisualBasic assembly to use that method. [Strings.Asc Method (String)](http://msdn.microsoft.com/en-us/library/78sb8546.aspx) – Tim Oct 13 '12 at 06:33
1

The problem is that you lose (never use) TempChar.

If you just make this change, you're home:

value = (TempChar - 120);
ronalchn
  • 12,225
  • 10
  • 51
  • 61
mawiks
  • 11
  • 1
0

You're never assigning the value of the character to value, so you're going to wind up with -120, -240, etc.

Here's another way to do it, without all the string manipulation:

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 "";
        }
    }
Tim
  • 28,212
  • 8
  • 63
  • 76