基本的函數
'用於將 一個變量 的類型打印出來。 Public Function getVarTypeToString(ByVal m_value As VbVarType) As String 'varType typename 'information: IsArray IsDate IsEmpty IsError IsMissing IsNULL isNumric IsObject Select Case m_value Case vbArray ', vbArray + vbByte, vbArray + vbInteger, vbArray + vbLong, vbArray + vbDouble: getVarTypeToString = "vbArray" Case vbBoolean getVarTypeToString = "vbBoolean" Case vbByte getVarTypeToString = "vbByte" Case vbCurrency getVarTypeToString = "vbCurrency" Case vbDataObject getVarTypeToString = "vbDataObject" Case vbDate getVarTypeToString = "vbDate" Case vbDecimal getVarTypeToString = "vbDecimal" Case vbDouble getVarTypeToString = "vbDouble" Case vbEmpty getVarTypeToString = "vbEmpty" Case vbError getVarTypeToString = "vbError" Case vbInteger getVarTypeToString = "vbInteger" Case vbLong getVarTypeToString = "vbLong" Case vbNull getVarTypeToString = "vbNull" Case vbObject getVarTypeToString = "vbObject" Case vbSingle getVarTypeToString = "vbSingle" Case vbString getVarTypeToString = "vbString" Case vbUserDefinedType getVarTypeToString = "vbUserDefinedType" Case vbVariant getVarTypeToString = "vbVariant" Case Else If m_value > 8192 Then getVarTypeToString = "vbArray + " & getVarTypeToString(m_value - 8192) Else getVarTypeToString = CDbl(m_value) & " is what varType ######### (in getVarTypeToString() ) # ? " End If End Select End Function
上面的函數可以將 各種類型的數組都打印出來。
其各種類型 都對應這一些數字,分別是:
string相關函數
Public Function showString(ByVal str As String) Dim i 'myDebug "str = " & str For i = 1 To Len(str) showString = showString & " " & Mid$(str, i, 1) Next myDebug showString End Function Public Function string2bytes(ByVal str As String) As Byte() Dim mulBits() As Byte Dim i ReDim Preserve mulBits(Len(str) - 1 )'從0開始的,如果不-1,則會在字節數組后面多出一個0 For i = 1 To Len(str) 'ReDim Preserve mulBits(i) mulBits(i - 1) = Asc(Mid(str, i, 1)) Next 'mulBits = str 'myDebug UBound(mulBits)'string是unicode,所以直接轉換會會變成雙倍個數 8 =》16 string2bytes = mulBits End Function Public Function bytes2string(ByRef bytes() As Byte) As String Dim str As String Dim i For i = 0 To UBound(bytes) bytes2string = bytes2string & Chr(bytes(i)) Next End Function
測試函數:
Dim str1 As String: str1 = "cnblogsCOM" showString str1 myDebug getHex(str1) myDebug getHex(string2bytes(str1)) myDebug getHex(bytes2string(string2bytes(str1))) myDebug getHex(string2bytes(bytes2string(string2bytes(str1)))) myDebug getHex(bytes2string(string2bytes(bytes2string(string2bytes(str1)))))
結果為:
==============================
c n b l o g s C O M
[getHex Type is vbString]
0x 63 6E 62 6C 6F 67 73 43 4F 4D
[getHex Type is vbArray + vbByte]
0x 63 6E 62 6C 6F 67 73 43 4F 4D
[getHex Type is vbString]
0x 63 6E 62 6C 6F 67 73 43 4F 4D
[getHex Type is vbArray + vbByte]
0x 63 6E 62 6C 6F 67 73 43 4F 4D
[getHex Type is vbString]
0x 63 6E 62 6C 6F 67 73 43 4F 4D
==============================
字節數組 和 integer long 整數的互相轉換
暫時沒有完成。
應該有 integer2bytes long2bytes
bytes2integer bytes2long
四個函數,分別 用 兩個字節,四個字節表示 integer和long(vb中integer就是兩個字節而已),沒有64位的類型。唉 弱弱的vb,與易學的vb真是不兼容啊。
2字節integer和4字節long與byte()互相轉換的函數為:
'###################################################################################################### '整數和字節數組 轉換 '###################################################################################################### Public Function BytesToInt2(ByRef b() As Byte) As Integer If UBound(b) < 1 Then MsgBox "#Error: in BytesToInt2(byte()),byte() is not 2 bytes!" End If Dim s0 As Integer: s0 = b(0) And &HFF& Dim s1 As Integer: s1 = b(1) And &HFF& 'SHL(s0,0) If Not SHL(s1, 8) Then MsgBox "#Error to SHL()" End If BytesToInt2 = s0 Or s1 End Function Public Function BytesToInt4(ByRef b() As Byte) As Long If UBound(b) < 3 Then MsgBox "#Error: in BytesToInt2(byte()),byte() is not 4 bytes!" End If Dim s0 As Long: s0 = b(0) And &HFF Dim s1 As Long: s1 = b(1) And &HFF Dim s2 As Long: s2 = b(2) And &HFF Dim s3 As Long: s3 = b(3) And &HFF 'MsgBox "Before bitOpt bytestoint4 s0-s1=" & Hex$(s0) & " " & Hex$(s1) & " " & Hex$(s2) & " " & Hex$(s3) & " " 'SHL(s0,0) If Not SHL(s1, 8) Then MsgBox "#Error to SHL()" End If If Not SHL(s2, 16) Then MsgBox "#Error to SHL()" End If If Not SHL(s3, 24) Then MsgBox "#Error to SHL()" End If 'MsgBox "After bitopt bytestoint4 s0-s1=" & Hex$(s0) & " " & Hex$(s1) & " " & Hex$(s2) & " " & Hex$(s3) & " " BytesToInt4 = s0 Or s1 Or s2 Or s3 End Function Public Function Int2ToBytes(ByVal vData As Integer) As Byte() Dim ret() As Byte Dim s0 As Integer, s1 As Integer s0 = vData And &HFF s1 = vData And &HFF00 If Not SHR(s1, 8) Then MsgBox "#ERROR:to SHR() " End If ReDim ret(1) ret(0) = s0 ret(1) = s1 Int2ToBytes = ret End Function Public Function Int4ToBytes(ByVal vData As Long) As Byte() Dim ret() As Byte Dim s0 As Long, s1 As Long, s2 As Long, s3 As Long s0 = vData And &HFF s1 = vData And &HFF00 If Not SHL(s1, 16) Then MsgBox "#ERRO to SHL()" End If If Not SHR(s1, 16) Then MsgBox "#ERRO to SHR()" End If s2 = vData And &HFF0000 s3 = vData And &HFF000000 'SHR(s0,8*0) If Not SHR(s1, 8 * 1) Then MsgBox "#ERROR:to SHR() " End If If Not SHR(s2, 8 * 2) Then MsgBox "#ERROR:to SHR() " End If If Not SHR(s3, 8 * 3) Then MsgBox "#ERROR:to SHR() " End If ReDim ret(3) ret(0) = s0 ret(1) = s1 ret(2) = s2 ret(3) = s3 Int4ToBytes = ret End Function Public Function BytesToLong(ByRef b() As Byte) As Long BytesToLong = BytesToInt4(b) End Function Public Function LongToBytes(ByVal vData As Long) As Byte() LongToBytes = Int4ToBytes(vData) End Function
其依賴與BitPlus.bas

Option Explicit 'Module: BitPlus.Bas '發信人:hermit (阿修羅~相擁我愛), 信區: VisualBasic '標 題: VB中位操作運算函數【移位指令】 '發信站:BBS 水木清華站 (Sat Jun 1 12:40:23 2002) 'Code By Hermit @ SMTH , Jun. 1st,2000 'Email: mailtocw@sohu.com 'May these functions will help you, and 'Please keep this header if you use my code,thanks! '提供在VB下進行位運算的函數 'SHL 邏輯左移 SHR 邏輯右移 'SAL 算術左移 SAR 算術右移 'ROL 循環左移 ROR 循環右移 'RCL 帶進位循環左移 RCR 帶進位循環右移 'Bin 將給定的數據轉化成2進制字符串 '使用方法 'SHL SHR SAL SAR ROL ROR 基本類似,以SHL為例說明 '可以移位的變量類型,字節(Byte),整數(Integer),長整數(Long) '返回值 True 移位成功, False 移位失敗,當對非上述類型進行移位是會返回False 'Num 傳引用變量,要移位的數據,程序會改寫Num的值為運算后結果 'iCL 傳值變量,要移位的次數,缺省值移位1次 '例 Dim A As Integer ' A = &H10 '如 SHL A 則移位后 A = &H20 '如 SHL A,2 則移位后 A = &H40 '如 SHL A,4 則移位后 A = &H00 'RCR與RCL類似,以RCL為例說明 '這里需要多給定一個參數,即第一次移位時的進位值iCF 'Bin舉例 'A = &H1 '如 A 為字節,則 Bin(A) 返回值為 "00000001" '如 A 為整數,則 Bin(A) 返回值為 "0000000000000001" '如 A 為長整數,則 Bin(A) 返回值為 "00000000000000000000000000000001" '如果傳入參數非上述類型時,返回值為 "" '更詳細的信息,請參考相關匯編書籍 Public Function testBitPlus() As String Dim testData As Integer Dim str As String testData = &HF000 str = str & "數據為:" & Bin(testData) & vbCrLf & vbCrLf If SHR(testData, 10) Then str = str & "SHR,10:" & Bin(testData) & " 邏輯右移" & vbCrLf End If testData = &HF000 If SAR(testData, 10) Then str = str & "SAR,10:" & Bin(testData) & " 算術右移" & vbCrLf & vbCrLf End If testData = &H100 str = str & "數據為:" & Bin(testData) & vbCrLf & vbCrLf If SHL(testData, 4) Then str = str & "SHL,04:" & Bin(testData) & " 邏輯左移" & vbCrLf End If If SHL(testData, 10) Then str = str & "SHL,10:" & Bin(testData) & " 邏輯左移" & vbCrLf & vbCrLf End If testData = &H100 If SHR(testData, 4) Then str = str & "SHR,04:" & Bin(testData) & " 邏輯右移" & vbCrLf End If testData = &H100 If SHR(testData, 10) Then str = str & "SHR,10:" & Bin(testData) & " 邏輯右移" & vbCrLf & vbCrLf End If testData = &H100 If SAL(testData, 4) Then str = str & "SAL,04:" & Bin(testData) & " 算術左移=邏輯左移" & vbCrLf & vbCrLf End If testData = &H100 If SAR(testData, 4) Then str = str & "SAR,04:" & Bin(testData) & " 算術右移" & vbCrLf End If testData = &H100 If SAR(testData, 10) Then str = str & "SAR,10:" & Bin(testData) & " 算術右移" & vbCrLf & vbCrLf End If testData = &H100 If ROL(testData, 4) Then str = str & "ROL,04:" & Bin(testData) & " 循環左移" & vbCrLf End If testData = &H100 If ROL(testData, 10) Then str = str & "ROL,10:" & Bin(testData) & " 循環左移" & vbCrLf & vbCrLf End If testData = &H100 If RCL(testData, 4) Then str = str & "RCL,04:" & Bin(testData) & " 帶進位循環左移" & vbCrLf End If testData = &H100 If RCL(testData, 10) Then str = str & "RCL,10:" & Bin(testData) & " 帶進位循環左移" & vbCrLf & vbCrLf End If testData = &H100 If ROR(testData, 4) Then str = str & "ROR,04:" & Bin(testData) & " 循環右移" & vbCrLf End If testData = &H100 If ROR(testData, 10) Then str = str & "ROR,10:" & Bin(testData) & " 循環右移" & vbCrLf & vbCrLf End If testData = &H100 If RCR(testData, 4) Then str = str & "RCR,04:" & Bin(testData) & " 帶進位循環右移" & vbCrLf End If testData = &H100 If RCR(testData, 10) Then str = str & "RCR,10:" & Bin(testData) & " 帶進位循環右移" & vbCrLf & vbCrLf End If testBitPlus = str & "結論:邏輯 算術左移一樣,右移按照最高位有區別 ; 循環 差不多,就差 邊緣的一位 " & vbCrLf & "推薦 使用 邏輯左右移 SHL SHR" End Function '邏輯左移 Public Function SHL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As Boolean Dim i As Byte Dim bMask As Byte, iMask As Integer, lMask As Long Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL iMask = 0 If (Num And &H4000) <> 0 Then iMask = &H8000 Num = (Num And &H3FFF) * 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL lMask = 0 If (Num And &H40000000) <> 0 Then lMask = &H80000000 Num = (Num And &H3FFFFFFF) * 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL bMask = 0 If (Num And &H40) <> 0 Then bMask = &H80 Num = (Num And &H3F) * 2 Or bMask Next Case Else SHL = False Exit Function End Select SHL = True End Function '邏輯右移 Public Function SHR(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As Boolean Dim i As Byte Dim bMask As Byte, iMask As Integer, lMask As Long Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL iMask = 0 If (Num And &H8000) <> 0 Then iMask = &H4000 Num = (Num And &H7FFF) \ 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL lMask = 0 If (Num And &H80000000) <> 0 Then lMask = &H40000000 Num = (Num And &H7FFFFFFF) \ 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL bMask = 0 If (Num And &H80) <> 0 Then bMask = &H40 Num = (Num And &H7F) \ 2 Or bMask Next Case Else SHR = False Exit Function End Select SHR = True End Function '算術左移 Public Function SAL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As Boolean SAL = SHL(Num, iCL) End Function '算術右移 Public Function SAR(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As Boolean Dim i As Byte Dim bMask As Byte, iMask As Integer, lMask As Long Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL iMask = 0 If (Num And &H8000) <> 0 Then iMask = &HC000 '和 邏輯 右移 區別就是 4000 => &HC00 0100 => 1100 Num = (Num And &H7FFF) \ 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL If (Num And &H80000000) <> 0 Then lMask = &HC0000000 Num = (Num And &H7FFFFFFF) \ 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL If (Num And &H80) <> 0 Then bMask = &HC0 Num = (Num And &H7F) \ 2 Or bMask Next Case Else SAR = False Exit Function End Select SAR = True End Function '循環左移 Public Function ROL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As Boolean Dim i As Byte Dim bMask As Byte, iMask As Integer, lMask As Long Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL iMask = 0 If (Num And &H4000) <> 0 Then iMask = &H8000 If (Num And &H8000) <> 0 Then iMask = iMask Or &H1 Num = (Num And &H3FFF) * 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL lMask = 0 If (Num And &H40000000) <> 0 Then lMask = &H80000000 If (Num And &H80000000) <> 0 Then lMask = lMask Or &H1 Num = (Num And &H3FFFFFFF) * 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL bMask = 0 If (Num And &H40) <> 0 Then bMask = &H80 If (Num And &H80) <> 0 Then bMask = bMask Or &H1 Num = (Num And &H3F) * 2 Or bMask Next Case Else ROL = False Exit Function End Select ROL = True End Function '循環右移 Public Function ROR(ByRef Num As Variant, Optional ByVal iCL As Byte = 1) As Boolean Dim i As Byte Dim bMask As Byte, iMask As Integer, lMask As Long Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL iMask = 0 If (Num And &H8000) <> 0 Then iMask = &H4000 If (Num And &H1) <> 0 Then iMask = iMask Or &H8000 Num = (Num And &H7FFF) \ 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL lMask = 0 If (Num And &H80000000) <> 0 Then lMask = &H40000000 If (Num And &H1) <> 0 Then lMask = lMask Or &H80000000 Num = (Num And &H7FFFFFFF) \ 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL bMask = 0 If (Num And &H80) <> 0 Then bMask = &H40 If (Num And &H1) <> 0 Then bMask = bMask Or &H80 Num = (Num And &H7F) \ 2 Or bMask Next Case Else ROR = False Exit Function End Select ROR = True End Function '帶進位循環左移 Public Function RCL(ByRef Num As Variant, Optional ByVal iCL As Byte = 1, Optional ByVal iCf As Byte = 0) As Boolean Dim i As Byte, CF As Byte Dim bMask As Byte, iMask As Integer, lMask As Long CF = iCf Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL If CF = 0 Then iMask = 0 Else iMask = 1 End If If (Num And &H4000) <> 0 Then iMask = iMask Or &H8000 If (Num And &H8000) <> 0 Then CF = 1 Else CF = 0 End If Num = (Num And &H3FFF) * 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL If CF = 0 Then lMask = 0 Else lMask = 1 End If If (Num And &H40000000) <> 0 Then lMask = lMask Or &H80000000 If (Num And &H80000000) <> 0 Then CF = 1 Else CF = 0 End If Num = (Num And &H3FFFFFFF) * 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL If CF = 0 Then bMask = 0 Else bMask = 1 End If If (Num And &H40) <> 0 Then bMask = bMask Or &H80 If (Num And &H80) <> 0 Then CF = 1 Else CF = 0 End If Num = (Num And &H3F) * 2 Or bMask Next Case Else RCL = False Exit Function End Select RCL = True End Function '帶進位循環右移 Public Function RCR(ByRef Num As Variant, Optional ByVal iCL As Byte = 1, Optional ByVal iCf As Byte = 0) As Boolean Dim i As Byte, CF As Byte Dim bMask As Byte, iMask As Integer, lMask As Long CF = iCf Select Case VarType(Num) Case 2 '16 bits For i = 1 To iCL If CF = 1 Then iMask = &H8000 Else iMask = 0 End If If (Num And &H8000) <> 0 Then iMask = iMask Or &H4000 If (Num And &H1) <> 0 Then CF = 1 Else CF = 0 End If Num = (Num And &H7FFF) \ 2 Or iMask Next Case 3 '32 bits For i = 1 To iCL If CF = 1 Then lMask = &H80000000 Else lMask = 0 End If If (Num And &H80000000) <> 0 Then lMask = lMask Or &H40000000 If (Num And &H1) <> 0 Then CF = 1 Else CF = 0 End If Num = (Num And &H7FFFFFFF) \ 2 Or lMask Next Case 17 '8 bits For i = 1 To iCL If CF = 1 Then bMask = &H80 Else bMask = 0 End If If (Num And &H80) <> 0 Then bMask = bMask Or &H40 If (Num And &H1) <> 0 Then CF = 1 Else CF = 0 End If Num = (Num And &H7F) \ 2 Or bMask Next Case Else RCR = False Exit Function End Select RCR = True End Function '將數值轉化為二進制字符串 Public Function Bin(ByVal Num As Variant) As String Dim tmpStr As String Dim iMask As Long Dim iCf As Byte, iMax As Byte Select Case VarType(Num) Case 2: iMax = 15 'Integer 16 bits Case 3: iMax = 31 'Long 32 bits Case 17: iMax = 7 'Byte 8 bits Case Else Bin = "" Exit Function End Select iMask = 1 If iMask And Num Then tmpStr = "1" Else tmpStr = "0" End If For iCf = 1 To iMax If iCf = 31 Then If Num > 0 Then tmpStr = "0" + tmpStr Else tmpStr = "1" + tmpStr End If Exit For End If iMask = iMask * 2 If iMask And Num Then tmpStr = "1" + tmpStr Else tmpStr = "0" + tmpStr End If If (iCf + 1) Mod 4 = 0 Then tmpStr = " " + tmpStr 'Debug.Print iCf & ":" & tmpStr End If Next Bin = tmpStr End Function
測試代碼
Dim int2_1 As Integer int2_1 = &HF0AC Dim str As String str = "integer(2字節)與字節 的轉換 " & vbCrLf & getHexOnly(int2_1) & vbCrLf & _ getHexOnly(Int2ToBytes(int2_1)) & vbCrLf & _ getHexOnly(BytesToInt2(Int2ToBytes(int2_1))) & vbCrLf & vbCrLf Dim int4 As Long int4 = &HFF00EEAA MsgBox str & "long(4字節)與字節 的轉換 " & vbCrLf & getHexOnly(int4) & vbCrLf & _ getHexOnly(Int4ToBytes(int4)) & vbCrLf & _ getHexOnly(BytesToInt4(Int4ToBytes(int4)))
可以看到經過兩次轉換,結果與原始數據相同!太成功了(圖片文字有點小錯誤,不再上傳圖片修改了)
將 各種類型的變量,以十六進制的形式打印出來
'基本的 補全0 Public Function hexfix(ByVal val As Byte) As String '補0 If val < 16 Then hexfix = "0" End If hexfix = hexfix & Hex$(val) End Function '只獲得十六進制,沒有類型 Public Function getHexOnly(ByVal val As Variant) As String getHexOnly = "0x " Select Case VarType(val) Case vbString: '8 getHexOnly = getHexOnly(string2bytes(val)) Case vbBytes '17 getHexOnly = getHexOnly & Hex$(val) Case vbByte + vbArray '17+8192 Dim i For i = 0 To UBound(val) getHexOnly = getHexOnly & hexfix(val(i)) & " " Next Case vbArray '8192 getHexOnly = getHexOnly & " " Case vbBoolean '11 getHexOnly = getHexOnly & " " Case vbInteger '2 getHexOnly = getHexOnly & " " Case vbLong '3 getHexOnly = getHexOnly & " " Case Decimal '14 getHexOnly = getHexOnly & " " Case vbDouble '5 getHexOnly = getHexOnly & " " Case vbEmpty '0 getHexOnly = getHexOnly & " " Case vbError '10 getHexOnly = getHexOnly & " " Case vbNull '1 getHexOnly = getHexOnly & " " Case vbObject '9 getHexOnly = getHexOnly & " " Case vbSingle '4 getHexOnly = getHexOnly & " " Case vbVariant '12 getHexOnly = getHexOnly & " " Case vbUserDefinedType '36 getHexOnly = getHexOnly & " " Case Else If VarType(val) > 8192 Then getHexOnly = getHexOnly & "[]- -[]" & getVarTypeToString(VarType(val)) Else MsgBox "What kind of val" & "(" & VarType(val) & "[8192 0x2000 是vbArray ]) that is " & TypeName(val) End If End Select End Function '不但打印類型,還有十六進制 Public Function getHex(ByVal val) As String getHex = "[getHex Type is " & getVarTypeToString(VarType(val)) & "] " & vbCrLf & getHexOnly(val) End Function
還沒有將所有的類型都寫完,基本的 bytes string integer long 有了。
其實 上面 也可以寫一個 getDecimalToString,就是按照每個字節,打印出來 整數,可以和十六進制對比。
要知道,搞清楚上面的這些函數,費的時間很長。。需要對vb進行測試。
vb來傳輸 Socket 數據包,按照字節編寫的那種,沒有這些基本函數怎么能行呢?