【问题标题】:Converting Strings to Hex/ByteArray VB将字符串转换为 Hex/ByteArray VB
【发布时间】:2016-06-04 04:33:59
【问题描述】:

我目前正在做一个项目,我需要输入一个简单的字符串:

string ("Example") 

并转换为十六进制字节数组:

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

在 VBA 中,我使用 strconv 实现了这一点:

bytes = StrConv(id, vbFromUnicode)

努力在 VB 中找到等效功能,到目前为止,我已经设法使用 Hex() 函数创建了一个等效整数,但如上所述,我需要将每个十六进制等效字符存储在一个字节数组中。可能是一个简单的解决方案,感谢所有帮助!

【问题讨论】:

  • I have managed to create an integer equivalent using Hex() 旧版 VB Hex() 函数返回字符串,而不是整数。无论您是否使用十六进制表示法,基础值都是相同的
  • 字节数组我觉得就够了...十六进制只是显示

标签: vb.net string hex bytearray


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2020-07-16
    • 2019-07-16
    • 2018-03-20
    相关资源
    最近更新 更多