Results 1 to 13 of 13

Thread: [RESOLVED] Convert Hex String to a normal String

Threaded View

  1. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Convert Hex String to a normal String

    The main speed for my function relies on avoiding memory copy: ie. I could have simply done Char = Hex, but this would have made a new byte array which I did not want. So, instead I added two API calls to allow pointing Char to the existing Hex string. All the validation code makes the function a little slower. Despite this it is two times faster than HexToAsc.

    If I remove all validation and space character handling (edit! includes space character handling):
    Code:
    Public Function HexANSIToStringFast2(ByVal Hex As String) As String
        Dim Char() As Byte, Header(0 To 5) As Long, Ptr As Long
        Dim B As Byte, I As Long, J As Long, S As Long, TL(0 To 255) As Byte, TU(0 To 255) As Byte
        For B = 49 To 57: TL(B) = B - 48: TU(B) = TL(B) * 16: Next
        For B = 65 To 70: TL(B) = B - 55: TU(B) = TL(B) * 16: TL(B + 32) = TL(B): TU(B + 32) = TU(B): Next
        Header(0) = 1: Header(1) = 1: Header(3) = StrPtr(Hex): Header(4) = LenB(Hex)
        Ptr = ArrayPtr(Char): PutMem4 Ptr, VarPtr(Header(0))
        If Char(4) <> 32 Then S = 4 Else S = 6
        For I = 0 To Header(4) - 4 Step S
            Char(J) = TU(Char(I)) Or TL(Char(I + 2))
            J = J + 1
        Next I
        PutMem4 Ptr, 0
        If J Then
            HexANSIToStringFast2 = StrConv(LeftB$(Hex, J), vbUnicode)
        Else
            Err.Raise 5
        End If
    End Function
    It gains a considerable amount of more speed: this works just like HexToAsc, but runs 3.5 times faster.


    Update!
    Project updated with fixed versions.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Merri; May 25th, 2010 at 11:25 PM. Reason: Bug fixes & added support for space separator; faster than anhn's function + gives valid results.

Posting Permissions

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



Click Here to Expand Forum to Full Width