从C#到VB.Net的位映射转换

时间:2014-04-04 03:57:04

标签: c# vb.net

我试图将一些C#代码转换为VB.Net。请参阅以下内容:

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);

来自观察者:format = 786433 int //这是正确的值。

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)

来自观察者:format = 13整数 &#39;&#39;这是不正确的值。

任何想法为什么?

感谢。

3 个答案:

答案 0 :(得分:1)

将常量更改为整数,得到与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

我不太确定为什么这是必要的,但以下帖子可能会有所启示: Binary Shift Differences between VB.NET and C#

另一个选择 - 也许更容易忍受,是保持常量相同,但只使用演员:

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

答案 1 :(得分:0)

我想你忘了在这行中指定数据类型

Private Const SO_IMAGE_FORMAT = SO_IMAGE_RAW

尝试将其更改为

Private Const SO_IMAGE_FORMAT As UShort = SO_IMAGE_RAW

编辑:

this也是将c#代码转换为vb.net

的绝佳工具

答案 2 :(得分:0)

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