当前位置: 代码迷 >> VB Dotnet >> VB.NET怎么用移位实现把一个integer截成4个byte
  详细解决方案

VB.NET怎么用移位实现把一个integer截成4个byte

热度:113   发布时间:2016-04-25 02:14:42.0
VB.NET如何用移位实现把一个integer截成4个byte?
RT

在C#里可以类似这样
int p= 2147483648
int p1 =(Byte)p>>24
int p2 = (Byte)p>>16
int p3 = (Byte)p>>8
int p4 = (Byte)p

请问一个(Byte)在vb里应该用什么替代?谢谢
------解决方案--------------------
System.BitConverter.GetBytes
------解决方案--------------------

cType(表达式,要转换的类型)
System.BitConverter.GetBytes

------解决方案--------------------
Imports System.Runtime.InteropServices

Module Module1

    Sub Main()
        Dim aInts() As Integer = {&H12345678, &H9ABCDEF0}
        Dim abytes() As Byte = Integers2Bytes(aInts)
        For i As Integer = 0 To abytes.Count - 1
            Debug.Write(String.Format("{0:X2}-", abytes(i)))
        Next
        Debug.WriteLine("")
    End Sub

    Function Integers2Bytes(ByVal aInts() As Integer) As Byte()
        Dim pt As New IntPtr
        pt = Marshal.AllocHGlobal(aInts.Count * 4)
        Marshal.Copy(aInts, 0, pt, aInts.Count)

        Dim aBytes(aInts.Count * 4 - 1) As Byte
        Marshal.Copy(pt, aBytes, 0, aBytes.Count)

        Marshal.FreeHGlobal(pt)

        Return aBytes
    End Function

End Module

78-56-34-12-F0-DE-BC-9A-
  相关解决方案