dimanche 13 avril 2014

Conversion de mappage de bit de c# vers VB.Net - Stack Overflow


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.




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)



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




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



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.



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)


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



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


0 commentaires:

Enregistrer un commentaire