vb vs c xoring

shardic

New member
Joined
Oct 31, 2006
Messages
3
Location
Glen Carbon, IL
Programming Experience
Beginner
I need to convert the following C code to VB such that the resulting bit configurations are identical. Note that I cannot alter the variable types.

Being relatively new to VB I am a little confused concerning XOR'in VB ints with chars. Any advice is greatly appreciated.

char *somefunc( char *instr, char *pwstr, long len )
{
int j, pwlen, passnum;
long i;
char buff[1];
pwlen = strlen( pwstr ); /* length of password */
/* get seed value */
passnum = (int) ((((_tr_pnum(pwstr)/997) - 1) % 254 ) + 1);
for ( i = j = 0; i < len; i++ ) /* process whole string */
{
passnum = (int) (((passnum + ( i - len )) - 1 ) % 254) + 1;
buff[0] = (instr ^ (passnum ^ pwstr[j]) ); /* XOR 3 var's */
instr = (buff[0] ? buff[0] : instr); /* if NULL retrn char */
j = ( j >= pwlen ? 0 : j++ ); /* password que control variable */
}

return(instr); /* send back encrypted string */
}
 
VB.NET:
Do While i < len ' process whole string 
passnum = CInt((passnum + (i - len)) - 1) Mod 254) + 1
buff(0) = (instr(i) Xor (passnum Xor pwstr(j))) ' XOR 3 var's 
If buff(0) Then
 instr(i) = (buff(0))
Else
 instr(i) = (instr(i))
End If
If j >= pwlen Then
 j = (0)
Else
 j += 1
End If
 i += 1
Loop

Well, above is my effort at converting that loop. This is where c# differs greatly from VB.Net. I haven't tested it, just done a syntactical translation. Let me know how it goes.
 
Thanks for the reply. This still has the issue of 'xor not defined for types Integer and char' for (passnum Xor pwstr(j))
This is where I get confused concerning the bit configuration if I convert the int to a char or the char to an int.
 
As i say i haven't been able to test it out myself. I'm assuming that you have tried to convert the values? I'd use the BitConverter Class to do it.
 
Thank you for that "little" clue. I have not been aware of the bitconverter class and that seems to be exactly what I needed. The process of converting all entities to BYTE and then xor'ing seems to yield the same results as executing the C program.

Again, thanks for your help.
 
Back
Top