簡體   English   中英

將字符串轉換為Hex / ByteArray VB

[英]Converting Strings to Hex/ByteArray VB

我目前正在從事要求我接受一個簡單字符串的項目:

string ("Example") 

並轉換為十六進制字節數組:

HexByteArray(45 ,78 ,61 ,6d ,70 ,6c ,65)

在VBA中,我使用strconv實現了這一點:

bytes = StrConv(id, vbFromUnicode)

努力尋找VB中的等效功能,到目前為止,我已經設法使用Hex()函數創建了一個整數等效項,但是如上所述,我需要將每個字符十六進制等效項存儲在一個字節數組中。 可能是一個簡單的解決方案,所有幫助表示贊賞!

Sub GetHexString(Value As String)
    Dim Bytes() As Byte = Text.Encoding.ASCII.GetBytes(Value)

    ' StringBuilder Capacity for 2 characters plus space per byte
    With New StringBuilder(Bytes.Length * 3)
        For Each B As Byte In Bytes
            ' note the trailing space in the format
            .AppendFormat("{0:x2} ", B)
        Next

        Debug.Print(.ToString.Trim)

        ' If you want an array of strings, split on the spaces
        Dim HexString() As String = Split(.ToString.Trim, " ")
    End With
End Sub

GetHexString(“ Example”)的輸出:

45 78 61 6d 70 6c 65

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM