Tiger
Tiger

Reputation: 45

Bit mapping conversion from C# to VB.Net

I'm trying to convert some C# code to VB.Net. See the following:

C#:

private const ushort SO_IMAGE_RAW = 1;
private const ushort SO_IMAGE_DIB = 2;
private const ushort SO_IMAGE_DCM = 3;
private const ushort SO_IMAGE_BITDEPTH = 12;
private const ushort SO_IMAGE_FORMAT = SO_IMAGE_RAW;
int format = (SO_IMAGE_BITDEPTH << 16) + (SO_IMAGE_FORMAT & 0x0000FFFF);

From the watcher: format=786433 int // This is correct value.

VB.Net:

Private Const SO_IMAGE_RAW As UShort = 1
Private Const SO_IMAGE_DIB As UShort = 2
Private Const SO_IMAGE_DCM As UShort = 3
Private Const SO_IMAGE_BITS As UShort = 12
Private Const SO_IMAGE_FORMAT = SO_IMAGE_RAW
Dim format As Integer = (SO_IMAGE_BITS << 16) + (SO_IMAGE_FORMAT And &HFFFF)

From the watcher: format=13 Integer '' This is incorrect value.

Any ideas why?

Thanks.

Upvotes: 1

Views: 193

Answers (3)

Dave Doknjas
Dave Doknjas

Reputation: 6542

Change the constants to integers and you get the same result as C#:

Private Const SO_IMAGE_RAW As Integer = 1
Private Const SO_IMAGE_DIB As Integer = 2
Private Const SO_IMAGE_DCM As Integer = 3
Private Const SO_IMAGE_BITDEPTH As Integer = 12
Private Const SO_IMAGE_FORMAT As Integer = SO_IMAGE_RAW

I'm not quite sure why this is necessary, but the following post might shed some light: Binary Shift Differences between VB.NET and C#

Another option - perhaps easier to stomach, is to keep the constants the same, but just use a cast:

Dim format As Integer = (CInt(SO_IMAGE_BITDEPTH) << 16) + (SO_IMAGE_FORMAT And &HFFFF)

Upvotes: 1

Mihai Hantea
Mihai Hantea

Reputation: 1743

Private Const SO_IMAGE_RAW As UShort = 1
Private Const SO_IMAGE_DIB As UShort = 2
Private Const SO_IMAGE_DCM As UShort = 3
Private Const SO_IMAGE_BITDEPTH As UShort = 12
Private Const SO_IMAGE_FORMAT As UShort = SO_IMAGE_RAW
Private format As Integer = (SO_IMAGE_BITDEPTH << 16) + (SO_IMAGE_FORMAT And &Hffff)

Convert C# to VB.NET link1

Convert C# to VB.NET link2

Upvotes: 0

Codemunkeee
Codemunkeee

Reputation: 1613

I think you forgot to specify the datatype in this line

Private Const SO_IMAGE_FORMAT = SO_IMAGE_RAW

try changing it to

Private Const SO_IMAGE_FORMAT As UShort = SO_IMAGE_RAW

EDIT:

this is also a great tool to convert c# code to vb.net

Upvotes: 0

Related Questions