1

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

6
  • 1
    what is the array url? Commented Feb 17, 2012 at 18:37
  • 2
    What type is the url array? Char? Also, what are you trying to achieve? Commented Feb 17, 2012 at 18:37
  • i got some info about the error that c# automatically truncates integer overflow, which vb.net does not. so it was suggested that i should turn off integer overflow check, but it still does not correct the error am getting Commented Feb 17, 2012 at 19:09
  • 1
    @MyK What was that reason, again? You know the syntax for C# but not for VB.NET? I fail to see how that's a language inadequacy... Commented Feb 17, 2012 at 23:40
  • Hello Smith. Were you able to successfully port this perfect hash function by Bob Jenkins to VB.Net? I am looking for this function in VB.Net. Commented Apr 20, 2012 at 6:18

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

i did that, an i got 'Char' values cannot be converted to 'UInteger'. Use 'Microsoft.VisualBasic.AscW' to interpret a character as a Unicode value or 'Microsoft.VisualBasic.Val' to interpret it as a digit.
Like I said, I didn't try or test it. Looks like you need AscW() instead of my inner CUInt().
i did this (CUInt(AscW(url(k + 1))) << 8) and got this error Operator '+' is not defined for types 'Char' and 'UInteger'.
Change every url(...) into AscW(url(...))
@Smith No offence, but I would like to be absolutely sure you really have changed every one. Because this compiles for me a += CUInt(AscW(url(k + 0)) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24)) but on the other hand this a += CUInt(url(k + 0) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24)) , where I have not wrapped url(k+0) in AscW, gives compile error Operator '+' is not defined for types 'Char' and 'Integer'
|
1

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

(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.

4 Comments

i tried it an got the same error 'Char' values cannot be converted to 'UInteger'. Use 'Microsoft.VisualBasic.AscW' to interpret a character as a Unicode value or 'Microsoft.VisualBasic.Val' to interpret it as a digit.
OK sounds like instead of using a CUInt cast you have to wrap it in a call to Val(url(k+2)) then shift the result.
thanks, how would i convert the first part url(k + 0), i did this url(k + 0) + Val(url(k)) + (Val(url(k + 2)) << 8), and i got another error Operator '+' is not defined for types 'Char' and 'Integer'. vb underlined this url(k + 0) + Val(url(k))
You have to wrap the first one also, see the first line of my example under EDIT int part_1 = Val(url(k));
0

http://msdn.microsoft.com/en-us/library/7haw1dex%28v=vs.71%29.aspx be sure that you use supported types.

1 Comment

throw more light on your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.