无论是何种类型,比如STRING,比如ASC码,我只想要该字节数据的二进制的值,应该怎么做

解决方案 »

  1.   

    Private Sub Command5_Click()
    Dim i As Byte
    i = 4
    MsgBox GetBinary(i)
    End Sub
    Private Function GetBinary(vData As Variant) As String
    Dim MyData As Long
    Dim S As String, Y As String
    Dim i As IntegerConst Bins = "0000000100100011010001010110011110001001101010111100110111101111"
    MyData = AscB(vData)
    Y = Hex(MyData)
    S = ""
    For i = 1 To Len(Y)
        S = S + Mid(Bins, (Val("&h" + Mid(Y, i, 1)) * 4 + 1), 4)
    Next i
    GetBinary = Trim(S) 'Format(s, "0")
    End Function
      

  2.   

    好像不对,比如说a的ASC码是61,那么它在计算机中的二进制就应该是111101,我就是想要这个二进制值
      

  3.   

    是这样的,我在作一个通讯程序,我要对接收到的数据作CRC校验,我想把所有收到的数据看作一个二进制序列
      

  4.   

    你可以GetBinary(chr(61))就是二进制的
      

  5.   

    Private Function getbinary(number As Integer) As String
    Dim binstr As String
    binstr = ""
    number = number + 1
    For x = 7 To 0 Step -1
    If number > 2 ^ x Then
    number = number - 2 ^ x
    binstr = binstr & "1"
    Else
    binstr = binstr & "0"
    End If
    Next
    getbinary = binstr
    End Function
    Private Sub Form_Load()
    Text1.Text = getbinary(567)
    Text2.Text = bintodec(Text1.Text)
    End Sub
    Private Function bintodec(binstr As String) As Integer
    Dim number As Integer
    For x = 0 To 7
    If Mid(binstr, x + 1, 1) = "1" Then
    number = number + 2 ^ (7 - x)
    End If
    Next
    bintodec = number
    End Function
      

  6.   

    楼上的,你的函数不是通用的,我要的是不分类型的,比如你的TEXT1输入一个字母就不对了
      

  7.   

    接收数据的时候 就是2进制的啊,如果是你用String来接收 话用 下面方法转化成 byte数组再进行CRC校验
    bytRcv=StrConv(strRcv,vbFromUnicode )
      

  8.   

    补充: dim bytRcv() as byte
    strRcv是你接收到的字符串
      

  9.   

    Option ExplicitPrivate Sub Command1_Click()
    Dim a As String
    Dim b As Stringa = GetBinary("abc")
    MsgBox a   ' 输出 二进制
    b = GetStrValue(a)
    MsgBox b   ' 输出 源字符
    End SubPrivate Function GetBinary(vData As String) As String '将字符换成二进制转的函数
    Dim MyData As Long
    Dim S As String, Y As String
    Dim I As IntegerConst Bins = "0000000100100011010001010110011110001001101010111100110111101111"
    MyData = AscB(vData)
    Y = Hex(MyData)
    S = ""
    For I = 1 To Len(Y)
        S = S + Mid(Bins, (Val("&h" + Mid(Y, I, 1)) * 4 + 1), 4)
    Next I
    GetBinary = Trim(S) 'Format(s, "0")
    End FunctionPrivate Function GetStrValue(vData As String) As String '将二进制转换成字符的函数
    Dim K As Long, D As Long, I As LongK = 1: D = 0
    For I = Len(vData) To 1 Step -1
        If Mid(vData, I, 1) = "1" Then D = D + K
        K = K * 2
    Next I
    GetStrValue = Chr(D)
    End Function
      

  10.   

    Modbus(CRC)校验例程
    Public Function CrcResult(ByVal Data As Long, ByVal Genpoly As Long, ByVal CrcData As Long) As LongDim TmpI As IntegerData = Data * 2For TmpI = 8 To 1 Step -1Data = Fix(Data / 2)
    If ((Data Xor CrcData) And 1) ThenCrcData = Fix(CrcData / 2) Xor GenpolyElseCrcData = Fix(CrcData / 2)End If
    Next TmpICrcResult = CrcDataEnd Function //函数应用方法Dim fx(7)Dim crc As Longfx(0) = Val(Text2.Text)fx(1) = &H3fx(2) = &H0fx(4) = &H0fx(5) = &H1fx(3) = MDI.Combo3.ListIndexcrc = &HFFFF&For TmpI = 0 To 5crc = CrcResult(CLng(fx(TmpI)), &HA001&, crc)Nextfx(6) = CByte(crc And &HFF&) ‘得到的校验高位fx(7) = CByte(Fix(crc / 256) And &HFF&) ‘得到的校验低位
      

  11.   

    正确答案是这样的,只要把MSComm1.InputMode= ComInputModeBinary,即用二进制方式接收,用一个BYTE数组来保存Buffer=MSComm1.Input,然后做CRC校验