Return binary from hex string-VBForums
Results 1 to 6 of 6

Thread: Return binary from hex string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Question Return binary from hex string

    Hi,

    I have a very old and big vbp program and the need to send a binary value to an external dll.

    Suposse the string strHex contains a long string of hex values.
    I need convert it to "binary" value.

    dim strHex as string
    strHex = "9D624714C76FB3646575E21504779F4AA1F231DB"

    .binary = CLng("&H" & strHex)
    or
    .binary = CDbl("&H" & strHex

    these samples returns error 13 type mismatch.

    I've read some about BSTR or Byte BSTR but haven't any code in vb6.

    Thanks very much!

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

  2. #2
    PowerPoster
    Join Date
    Oct 2013
    Posts
    2,910

    Re: Return binary from hex string

    This hexadecimal string is long to convert it in a single go.
    What kind of "binary" do you need? An array of bytes?

  3. #3
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,605

    Re: Return binary from hex string

    Here's a slow snippet I use, that gets the job done.

    Code:
    Private Function HexBytes(HexStr As String) As Byte()
        ReDim Bytes(0 To Len(HexStr) \ 2 - 1) As Byte
        Dim i As Long, j As Long
        For i = 1 To Len(HexStr) Step 2
            Bytes(j) = CByte("&H" & Mid$(HexStr, i, 2))
            j = j + 1
        Next
        HexBytes = Bytes
    End Function

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Return binary from hex string

    Quote Originally Posted by Arnoutdv View Post
    This hexadecimal string is long to convert it in a single go.
    What kind of "binary" do you need? An array of bytes?
    I think yes: array of bytes

    Thanks!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Return binary from hex string

    Quote Originally Posted by DEXWERX View Post
    Here's a slow snippet I use, that gets the job done.

    Code:
    Private Function HexBytes(HexStr As String) As Byte()
        ReDim Bytes(0 To Len(HexStr) \ 2 - 1) As Byte
        Dim i As Long, j As Long
        For i = 1 To Len(HexStr) Step 2
            Bytes(j) = CByte("&H" & Mid$(HexStr, i, 2))
            j = j + 1
        Next
        HexBytes = Bytes
    End Function
    Thanks very much for your code!
    I will test and reply here.


  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Return binary from hex string

    Hi DEXWERX,
    Thanks very much, your code is working

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.