Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stupid Array...

Status
Not open for further replies.

napjunk

Technical User
Dec 20, 2001
11
US
hey, me again (lol), this time it's arrays...my friend sent me some C source that he said i could use in my project.but after about an hour or so of messing with it i havn't been able to get anywhere...I don't know how to use arrays very well and this is way over my head, unfortunatley he doesn't know VB and can't help me with this...this part of the program right here is very important, and if i can't get it to work then everything else is useless....could someone PLEASE help me with this?

// -------------------------------------------------------------------
// Input: szName: null terminated string to be encrypted
//
// Output: will output to the specified buffer szRslt. The buffer
// must be twice as large as the largest possible input string.
//
void NapsterEncrypt(Char * szName, Char * szRslt)
{
unsigned int n;
static unsigned char chKeys[10] = {0x35, 0x0d, 0x54, 0x21, 0x5a, 0x20, 0x53, 0x08, 0x03, 0x04};

for (n=0; n < strlen(szName); n++)
sprintf(&szRslt[2*n], &quot;%02x&quot;, szName[n] ^ chKeys[n%10]); // encrypt

szRslt[2*n] = '\0'; // terminate
return;
}
 
Function Declaration in C:
Int MyFunc(Int x,Char *a)
{
stuff....
}

Function declaration in VB:
Function MyFunc(ByVal x as integer,ByRef a as byte) as integer

stuff...
end function

I)C
void NapsterEncrypt(Char * szName, Char * szRslt)
I)Meaning
Void= declaring a function return nothing;in VB use sub
Char *szName= a 1 byte pointer variable;in VB use ByRef
Char *szName= a 1 byte pointer variable; in VB use ByRef



unsigned int n; //2 byte integer thats all positive

static unsigned char chKeys[10] = {0x35, 0x0d, 0x54, 0x21, 0x5a, 0x20, 0x53, 0x08, 0x03, 0x04};

Static=variable that stays in memory after function ends.
in VB put variable in module as public
chKeys[10]= an array thats 10 elements long with first element 0x35. second element 0x0d.
0x35=hex number ox is to tell the compiler that number is hex; 0x35= 3*16+5 0x54=5*16+4 a=10 b=11 c=12 d=13 etc..


for (n=0; n < strlen(szName); n++) //in C
For n=0 to strlen(szName)-1 //in VB

n++ means n=n+1
strlen is a C function that returns the lengh of a string in bytes( in C, each character is a byte-in VB each character is 2 bytes)


sprintf(&szRslt[2*n], &quot;%02x&quot;, szName[n] ^ chKeys[n%10]);
sprintf=string to string print function-its form is:
sprintf(@VarOut,&quot;ControlString&quot;,VarIn)
@szRslt[2*n]=@ is address of var needed to output to szrslt which is an array indexed at twice n.
&quot;%02x&quot;=% in this instance is the print specification 02 means 2 characters(2 bytes) and x means as a hex number.
szNzme[n]^chkey[n%10]=^ is exclusive or.
0^0=0
0^1=1
1^0=1
1^1=0
n%10= is remainder (n mod 10 in VB)


szRslt[2*n] = '\0'; // this places the end of string character at the end of array. this is needed in C.
'\n'=end of string character

return; this is used to output values from function. Since this function returns nothing, the return staement is not needed
}
I don't know all the vb functions you'll need but the basic translation of your function would be:

'place this in a module
public n as integer
public chKeys(10) as byte
'load the values 0x35 etc.. somewhere before calling the function


Sub NapsterEncrypt(byref szName() as byte, byref szRslt() as integer)
dim n as iteger
dim Max as integer
dim Select as integer
Max=Len(szName) 'I forgot VB stringlength func name

for n=0 to Max-1
Select=n mod 10

szRslt(n)=szName(n) Xor chKeys(Select)
next n

end sub

Best wishes
 
what do you mean Load the hex values somewhere? You gotta remember i'm REALLY new to this and need to be &quot;spoon fed&quot; so to speak...
 
Place in the formload event

Form1_Load()
chKeys(0)=&H35
chKey(1)=&H0d
chKeys(2)=&H54
chKeys(3)=&H21
&quot;&quot;&quot;&quot;
&quot;&quot;&quot;&quot;
&quot;&quot;&quot;&quot;
chKeys(9)=&H04

'otherstuff can go here

End Sub

If your that new at it, I think you should try something a little easier and work your way up to it. Not to be mean, but after all, you alone will to have to debug the thing and data manipulatioon code is very hard to debug. Are you ready for that part of it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top