根据MSDN,Short数据类型由两个字节组成: https://msdn.microsoft.com/en-us/library/47zceaw7.aspx 但是如果我定义一个Short变量,内容总是4个字节: HFFFFFFFF Dim crc As Short = CShort(HFFFFS) ' crc = HFFFFFFFFDim cr
但是如果我定义一个Short变量,内容总是4个字节:& HFFFFFFFF
Dim crc As Short = CShort(&HFFFFS) ' crc = &HFFFFFFFF Dim crc As Short = &HFFFFS ' crc = &HFFFFFFFF
这句话甚至给我一个错误:
Dim crc As Short = CShort(&HFFFF) ' Error: Constant expression not representable in type 'Short'
这有什么用?为什么我的Short不占用两个字节?
MWE补充道:
Public Function CRC16(ByVal dataFrame As Byte(), ByVal dataLength As Int16) As Int16 Dim index As Int16 Dim crc As Short = &HFFFFS For iCount As Int16 = 0 To CShort(dataLength - 1) index = (crc >> 8) Xor dataFrame(iCount) crc = CShort(&HFFFF And ((crc << 8) Xor CRC_Table(index))) Next Return crc End Function这是因为Short是有符号的,因此最重要的位是为符号保留的.因此,您可以在签名短片中存储的最高值是& H7FFF或
Int16.MaxValue
如果要使用全部16位,则需要使用无符号短路(UInt16)
所以这个失败:
Dim crc As Short = CShort(&HFFFF)
但是这个作品:
Dim crc As UShort = CUShort(&HFFFF)