本文介绍了如何转换十六进制字符串成VB6字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的字节数组。

Dim Template(1023) As Byte

然后我打电话指纹扫描仪​​设备的功能,并返回以下内容:

Then i call the fingerprint scanner device function and returns the following:

Template(0) = 70
Template(1) = 77
Template(2) = 82
...
Template(1023) = 0

然后我的字节数组转换成字符串六角如下(查看附加的图像):

Then i convert the Bytes array into a string hex as follows (view attached image):

Dim n As Long, i As Long
ByteArrayToHexStr = Space$(3 * (UBound(Template) - LBound(Template)) + 2)
n = 1
For i = LBound(Template) To UBound(Template)
     Mid$(ByteArrayToHexStr, n, 2) = Right$("00" & Hex$(b(i)), 2)
    n = n + 3
Next


我怎样才能十六进制字符串转换成字节数组一遍吗?

Byte Array converted into Hex StringHow can i convert the hex string into a byte array again?

谢谢!

推荐答案

手工录入的花式版本宽容的:

Fancy version tolerant of manual entry:

Private Function BytesToHex(ByRef Bytes() As Byte) As String
    'Quick and dirty Byte array to hex String, format:
    '
    '   "HH HH HH"

    Dim LB As Long
    Dim ByteCount As Long
    Dim BytePos As Integer

    LB = LBound(Bytes)
    ByteCount = UBound(Bytes) - LB + 1
    If ByteCount < 1 Then Exit Function
    BytesToHex = Space$(3 * (ByteCount - 1) + 2)
    For BytePos = LB To UBound(Bytes)
        Mid$(BytesToHex, 3 * (BytePos - LB) + 1, 2) = _
            Right$("0" & Hex$(Bytes(BytePos)), 2)
    Next
End Function

Private Function HexToBytes(ByVal HexString As String) As Byte()
    'Quick and dirty hex String to Byte array.  Accepts:
    '
    '   "HH HH HH"
    '   "HHHHHH"
    '   "H HH H"
    '   "HH,HH,     HH" and so on.

    Dim Bytes() As Byte
    Dim HexPos As Integer
    Dim HexDigit As Integer
    Dim BytePos As Integer
    Dim Digits As Integer

    ReDim Bytes(Len(HexString) \ 2)  'Initial estimate.
    For HexPos = 1 To Len(HexString)
        HexDigit = InStr("0123456789ABCDEF", _
                         UCase$(Mid$(HexString, HexPos, 1))) - 1
        If HexDigit >= 0 Then
            If BytePos > UBound(Bytes) Then
                'Add some room, we'll add room for 4 more to decrease
                'how often we end up doing this expensive step:
                ReDim Preserve Bytes(UBound(Bytes) + 4)
            End If
            Bytes(BytePos) = Bytes(BytePos) * &H10 + HexDigit
            Digits = Digits + 1
        End If
        If Digits = 2 Or HexDigit < 0 Then
            If Digits > 0 Then BytePos = BytePos + 1
            Digits = 0
        End If
    Next
    If Digits = 0 Then BytePos = BytePos - 1
    If BytePos < 0 Then
        Bytes = "" 'Empty.
    Else
        ReDim Preserve Bytes(BytePos)
    End If
    HexToBytes = Bytes
End Function

这篇关于如何转换十六进制字符串成VB6字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:53