Converting C# code to VB.NET

后端 未结 3 1413
暗喜
暗喜 2021-01-16 03:52

i tried converting this code from c#

a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
相关标签:
3条回答
  • 2021-01-16 04:15

    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.

    0 讨论(0)
  • 2021-01-16 04:18

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

    0 讨论(0)
  • 2021-01-16 04:20

    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.

    0 讨论(0)
提交回复
热议问题