Help convert a string modification code from C++ to VB
Hi all.
I have written a C++ code to modify a string, the code is:
Code:
int Len = (nString.length());
char *szString = (char*)(nString.c_str()); // Convert to C string
for (int i = 0; i < Len; i++)
{
*(szString+i) = (*(szString+i) + 123); //XOR
}
Can someone help me convert this to VB:
I have this so far, but it is not working:
Code:
Dim i As Integer
Dim chrArray() As Char
chrArray = EncryptIn.ToCharArray
For intCounter = 0 To chrArray.Length - 1
chrArray(i) = chrArray(i + 123)
Next
Thanks bunch!
Re: Help convert a string modification code from C++ to VB
What does "it is not working" mean exactly? Do you get an error? Does something occur that you're not expecting? What are you expecting?
Re: Help convert a string modification code from C++ to VB
The line within the loop should be:
Code:
chrArray(i) = chrArray(i) + 123
Nah - that doesn't compile - I have no idea what you're trying to do with that original C++ code - could you explain?
Re: Help convert a string modification code from C++ to VB
Does this work?? Or does VB throw an overflow??
Code:
charArray(i) += ChrW(123)
In C# you could use this
Code:
charArray[i] += (char)123;
And I think the C++ could be rewritten as
Code:
szString[i] += 123;
Re: Help convert a string modification code from C++ to VB
I think the loop line should read:
Code:
chrArray(i) = Chr(Asc(chrArray(i)) Xor 123)
?
Re: Help convert a string modification code from C++ to VB
Quote:
Originally Posted by
David Anton
The line within the loop should be:
Code:
chrArray(i) = chrArray(i) + 123
Nah - that doesn't compile - I have no idea what you're trying to do with that original C++ code - could you explain?
Quote:
Originally Posted by
weirddemon
What does "it is not working" mean exactly? Do you get an error? Does something occur that you're not expecting? What are you expecting?
Thanks for helping guys, what I expect is to do some simple mathematical operation to the char bytes to encrypt the string
for example:
If I have string = "abc", and when I do this (in C++)
Code:
for (int i = 0; i < Len; i++)
{ *(szString+i) = (*(szString+i)+1); }
The string will become "bcd", so obviously for the real code I will have much more complicated formula to generate the new string
I hope this makes sense now, basically is the ability to do char # of each character one by one in the string
Re: Help convert a string modification code from C++ to VB
Quote:
Originally Posted by
Evil_Giraffe
I think the loop line should read:
Code:
chrArray(i) = Chr(Asc(chrArray(i)) Xor 123)
?
Hello.
I tested with
Code:
chrArray(i) = Chr(Asc(chrArray(i)) + 1)
and that is what I wanted, however when I tried to do more complicated calculation, such as Chr(Asc(chrArray(i)) Xor 123456) I got error of "Procedure call or argument is not valid"
Any idea why? Seems the number is outside of Chr()'s range (max 65536)? I actually do want to xor with a huge number, in c++ it works.
thanks!
Re: Help convert a string modification code from C++ to VB
Sorry about bumping this myself, really in need of a solution to this
basically is how to get chr() of value greater than 65536?
thanks!
Re: Help convert a string modification code from C++ to VB
Basically you can't, and you can't in C++ either.
Your question could be reworded as 'How can I fit more than 2 bytes into 2 bytes'
C++ allows you to overflow numbers silently, VB.net doesn't.
If you want to Xor a char with a large number try using a bitwise And &hFFFF with it first to truncate it.