简体   繁体   中英

Converting C# code to VB.NET

i tried converting this code from c#

a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));

to vb,net i get this

a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))

an i get this error

Operator '<<' is not defined for types 'Char' and 'Integer'.

Can anyone help me with a correction

EDIT

uint a, b;

a = b = 0x9E3779B9;

url = string

Your main problem seems to be that C# will allow bit-shifting on a char whereas VB does not.

So you would need something like (untested)

 CUInt( ... + (CUint( url(k + 1) ) << 8) + ... )

But it does look like a rather weak HashCode.

I don't know VB but I would suspect you can just cast each url(k+n) first ie

(CUint(url(k+2))<< 8)

I'm also assuming a CUint is 32 bits Assuming you are trying to create a 32 bit int out of 4 chars there is probably more checking you can do but at a minimum I would turn this into two methods ConvertCharArrayToUint() and another one that does each shift ShiftCharLeft(char, numBits) and hide all the casting ugliness in there. I'm surprised in C# you can shift a char like that.

EDIT: Try doing this on separate lines while you're figuring it out

int part_0 = Val(url(k));
int part_1 = Val(url(k+1));
int part_2 = Val(url(k+2));
...
int shifted_1 = part_1 << 8;
...
int result = part_0 + shifted_1...

They you can step through with the debugger, check types etc and get a full understanding of what is going on, then you can refactor for whatever readability you prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM