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

2
  • 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;. Commented Oct 13, 2012 at 6:17
  • thnx for helping me out friends Commented Oct 13, 2012 at 7:11

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

1 Comment

OP will need to add a reference to the Microsoft.VisualBasic assembly to use that method. Strings.Asc Method (String)
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.

4 Comments

your code is also not working. The resulted string is showing ]̤ɤ character in place of = sign. Thnx anyway
I don't know what you are decoding, I'm just using your formula to do it :-)
@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.
Hi Abdias! While encrypting 120 is added and while decrypting 120 is subtracted. The code is same
1

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

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

value = (TempChar - 120);

4 Comments

I don't think that will work - you need to cast TempChar to an int.
Works for me. I thought I'd had to cast, but didn't.
I'll be darned. There must be an implicit cast going on behind the scenes.
+1 for the shortest code solution :) Though it never hurts to be explicit in your code for the devs coming behind you.
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 "";
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.