Change code lenguage

Posts 1–12 of 12 · Page 1 of 1
Change code lenguage
Hello guys,

I have some problems converting 2 C++ functions in 2 vb.net ones.

Those are the functions:

Code:
char* Crypt(char string[], int key)
{
	int length = strlen(string);
	char* tempstring = new char[length + 1];
	for (int i = 0; i < length; i ++)
	{
		int KeyStep = key + i;
		tempstring[i] = string[i] + KeyStep;
	}
	tempstring[length] = 0;
	return tempstring;
}
char* Decrypt(char string[], int key)
 {
	int length = strlen(string);
	char* tempstring = new char[length + 1];
	for (int i = 0; i < length; i ++)
	{
		int KeyStep = key + i;
		tempstring[i] = string[i] - KeyStep;
	}
	tempstring[length] = 0;
	return tempstring;
}
I tryed to convert them in vb.net but I can't get the way to convert each char in his ASCII value in order to add the keystep and get the encrypted one:

Code:
Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa

        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            tempstring(i) = stringa(i).ToString + KeyStep.ToString
        Next

        Return tempstring

    End Function

    Public Function Decrypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa

        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            tempstring(i) = stringa(i).ToString - KeyStep.ToString
        Next

        Return tempstring

    End Function
Moreover, in the Decrypt function, at line 22
Code:
 tempstring(i) = stringa(i).ToString - KeyStep.ToString
I got this error: "Error 1 Value of type 'Double' cannot be converted to 'Char'. "


Can anyone help me?

Thanks in advance and have a nice day
try this
Code:
Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa

        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            tempstring(i) = stringa(i).ToString + KeyStep.ToString
        Next

        Return tempstring

    End Function

    Public Function Decrypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa
        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            Dim g As Double = stringa(i).ToString - KeyStep.ToString
            tempstring(i) = Convert.ToChar(g)
        Next

        Return tempstring

    End Function
@OP


Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

Dim length As Integer = stringa.Length
Dim tempstring() As Char = stringa '' Assigns the reference, not make a copy.

For i As Integer = 0 To length - 1
Dim KeyStep As Integer = key + i
tempstring(i) = stringa(i).ToString + KeyStep.ToString '' Changes the original!
''^^ char = string , idk.
Next
Return tempstring ''Function returns String, or Char array ?
End Function
Quote Originally Posted by abuckau907 View Post
@OP


Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

Dim length As Integer = stringa.Length
Dim tempstring() As Char = stringa '' Assigns the reference, not make a copy.

For i As Integer = 0 To length - 1
Dim KeyStep As Integer = key + i
tempstring(i) = stringa(i).ToString + KeyStep.ToString '' Changes the original!
''^^ char = string , idk.
Next
Return tempstring ''Function returns String, or Char array ?
End Function
Dim tempstring() As Char = stringa

Is done to get the same length as stringa






tempstring(i) = stringa(i).ToString + KeyStep.ToString

How can it changes the original?! The equation is meant to modify the tempstring.





Return tempstring

Your're right, I forgot to change the Function type... I was doing some tryes


Quote Originally Posted by nikit6000 View Post
try this
Code:
Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa

        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            tempstring(i) = stringa(i).ToString + KeyStep.ToString
        Next

        Return tempstring

    End Function

    Public Function Decrypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa
        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            Dim g As Double = stringa(i).ToString - KeyStep.ToString
            tempstring(i) = Convert.ToChar(g)
        Next

        Return tempstring

    End Function
The first Function is the same as mine one wich doesn't works...
The second function doesn't works:

vzx(the string to decrypt) -> Invalid cast from string "v" to type 'Double'.

I got this arror at this line (of course): Dim g As Double = stringa(i).ToString - KeyStep.ToString

---------- Post added at 11:27 AM ---------- Previous post was at 11:06 AM ----------

Omg I'm so stupid...
I solved this problem just converting the char of the original string to his corresponding ascii value, then added the key step and finally converted the sum in a char.

If anyone need them, those are the function:

Code:
Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa

        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            tempstring(i) = Chr(Asc(stringa(i)) + KeyStep)
        Next

        Return tempstring

    End Function
    Public Function Decrypt(ByVal stringa() As Char, ByVal key As Integer) As String

        Dim length As Integer = stringa.Length
        Dim tempstring() As Char = stringa
        For i As Integer = 0 To length - 1
            Dim KeyStep As Integer = key + i
            tempstring(i) = Chr(Asc(stringa(i)) - KeyStep)
        Next

        Return tempstring

    End Function
Quote Originally Posted by Sixx93 View Post
...
Dim tempstring() As Char = stringa

Is done to get the same length as stringa



tempstring(i) = stringa(i).ToString + KeyStep.ToString

How can it changes the original?! The equation is meant to modify the tempstring.
Value types vs Reference Types

I'm not going to even try to explain it...I hope you understand pointers, it's basically the same thing.
Arrays, in .net, are actually a reference type (as are all object of a class type), so when you set one = to another, you're just changing where the variable name points to. Sorry I can't explain better, you should probably read a proper tut. about it.

-------------------------------------------------------------------------------------------------------
Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

Dim length As Integer = stringa.Length
Dim tempstring() As Char = stringa '// still assigning a reference

For i As Integer = 0 To length - 1
Dim KeyStep As Integer = key + i
tempstring(i) = Chr(Asc(stringa(i)) + KeyStep)
Next

Return tempstring '// Place a breakpoint on this line (click to the left of it, a red ball will appear).
'// to verify. Hover mouse over variable name to see it's current value.


End Function

---------- Post added at 07:59 PM ---------- Previous post was at 07:55 PM ----------

Also called "value semantics" vs "reference semantics" . Arrays are a reference. Debug and check if it's still changing the original. A msgbox() would do.

--Dim tempstring() As Char = stringa

Dim tempstring(stringa.Length -1) As Char
Array.Copy(***) maybe?
Quote Originally Posted by abuckau907 View Post
Value types vs Reference Types

I'm not going to even try to explain it...I hope you understand pointers, it's basically the same thing.
Arrays, in .net, are actually a reference type (as are all object of a class type), so when you set one = to another, you're just changing where the variable name points to. Sorry I can't explain better, you should probably read a proper tut. about it.

-------------------------------------------------------------------------------------------------------
Public Function Crypt(ByVal stringa() As Char, ByVal key As Integer) As String

Dim length As Integer = stringa.Length
Dim tempstring() As Char = stringa '// still assigning a reference

For i As Integer = 0 To length - 1
Dim KeyStep As Integer = key + i
tempstring(i) = Chr(Asc(stringa(i)) + KeyStep)
Next

Return tempstring '// Place a breakpoint on this line (click to the left of it, a red ball will appear).
'// to verify. Hover mouse over variable name to see it's current value.


End Function

---------- Post added at 07:59 PM ---------- Previous post was at 07:55 PM ----------

Also called "value semantics" vs "reference semantics" . Arrays are a reference. Debug and check if it's still changing the original. A msgbox() would do.

--Dim tempstring() As Char = stringa

Dim tempstring(stringa.Length -1) As Char
Array.Copy(***) maybe?
Yeah, I understand what you are talking about... It's like C++ pointers... When you put them = to something you change the pointed memory not the real variable.


I think that the reason why I don't see this problem is that I pass at the function a TextBox.Text as Stringa(), so the function doesn't change the proper text of the textbox
How in the name of fuck does it take so long to figure this out:

Code:
Public Shared Function Crypt(ByVal raw As String, ByVal key As Integer) As String
    Dim sb As New StringBuilder(raw.Length + 1)
    For i As Integer = 0 To raw.Length - 1
        sb.Append(Convert.ToChar(Convert.ToInt32(raw(i)) + (key + i)))
    Next
    Return sb.ToString()
End Function

Private Function Decrypt(ByVal crypted As String, ByVal key As Integer) As String
    Dim sb As New StringBuilder(crypted.Length + 1)
    For i As Integer = 0 To crypted.Length - 1
        sb.Append(Convert.ToChar(Convert.ToInt32(crypted(i)) - (key + i)))
    Next
    Return sb.ToString()
End Function
Quote Originally Posted by Jason View Post
How in the name of fuck does it take so long to figure this out:

Code:
Public Shared Function Crypt(ByVal raw As String, ByVal key As Integer) As String
    Dim sb As New StringBuilder(raw.Length + 1)
    For i As Integer = 0 To raw.Length - 1
        sb.Append(Convert.ToChar(Convert.ToInt32(raw(i)) + (key + i)))
    Next
    Return sb.ToString()
End Function

Private Function Decrypt(ByVal crypted As String, ByVal key As Integer) As String
    Dim sb As New StringBuilder(crypted.Length + 1)
    For i As Integer = 0 To crypted.Length - 1
        sb.Append(Convert.ToChar(Convert.ToInt32(crypted(i)) - (key + i)))
    Next
    Return sb.ToString()
End Function
the stringbuilder gave me some errors. Anyway this topic is solved.

/request close
Quote Originally Posted by Sixx93 View Post
the stringbuilder gave me some errors. Anyway this topic is solved.

/request close
You'll need to reference the System.Text namespace, obviously.

Code:
Imports System.Text
Quote Originally Posted by Sixx93 View Post
the stringbuilder gave me some errors. Anyway this topic is solved.

/request close
Generally you can google any className + ".NET" and first result will tell you the Namspace it's in.
Replace Dim sb As New StringBuilder(crypted.Length + 1) with Dim sb As New System.Text.StringBuilder(crypted.Length + 1), same with the other occurrence.
Quote Originally Posted by Jason View Post


You'll need to reference the System.Text namespace, obviously.

Code:
Imports System.Text
of course, but when i declared the string as stringbuilder it said me that the class didn't have that member
Posts 1–12 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us