Results 1 to 9 of 9

Thread: Hex and String Conversions

  1. #1

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Hex and String Conversions

    Here is my Hex to String function

    Hex to String Function Code:
    1. Public Function HexToString(ByVal HexToStr As String) As String
    2. Dim strTemp   As String
    3. Dim strReturn As String
    4. Dim I         As Long
    5.     For I = 1 To Len(HexToStr) Step 3
    6.         strTemp = Chr$(Val("&H" & Mid$(HexToStr, I, 2)))
    7.         strReturn = strReturn & strTemp
    8.     Next I
    9.     HexToString = strReturn
    10. End Function

    and here is my String to Hex function

    String to Hex Function Code:
    1. Public Function StringToHex(ByVal StrToHex As String) As String
    2. Dim strTemp   As String
    3. Dim strReturn As String
    4. Dim I         As Long
    5.     For I = 1 To Len(StrToHex)
    6.         strTemp = Hex$(Asc(Mid$(StrToHex, I, 1)))
    7.         If Len(strTemp) = 1 Then strTemp = "0" & strTemp
    8.         strReturn = strReturn & Space$(1) & strTemp
    9.     Next I
    10.     StringToHex = strReturn
    11. End Function

    I hope they help everyone

  2. #2
    New Member
    Join Date
    Jul 2006
    Posts
    3

    Exclamation Re: Hex and String Conversions

    in the hex to string converter, that for should have a step of 2, not 3. Hope that helps someone cause it drove me nuts

  3. #3

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Hex and String Conversions

    3 will do perfectly fine.

  4. #4
    New Member
    Join Date
    Jul 2006
    Posts
    3

    Re: Hex and String Conversions

    3 just gave me gibberish, but 2 works great...weird

  5. #5
    Junior Member
    Join Date
    Feb 2007
    Posts
    28

    Re: Hex and String Conversions

    3 works fine for me. 2 gives me something entirely off.

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

    Re: Hex and String Conversions

    You're giving different strings: the code in first post works with space separated codes.

    Hex strings as such aren't very useful though.

  7. #7
    New Member
    Join Date
    Jul 2006
    Posts
    3

    Re: Hex and String Conversions

    That makes sense, there are no spaces in my hex string.

  8. #8
    New Member
    Join Date
    Aug 2017
    Posts
    1

    Re: Hex and String Conversions

    Quote Originally Posted by Paul M View Post
    Here is my Hex to String function

    I hope they help everyone

    Code:
    For anybody wondering, here it is for Excel VBA
    
    Public Function HexToString(ByVal HexToStr As String) As String
    
    Dim strTemp   As String
    Dim strReturn As String
    Dim I As Long
    
    For I = 1 To Len(HexToStr) Step 2 'edit: 2 instead of 3
        strTemp = Chr$(Val("&H" & Mid$(HexToStr, I, 2)))
        strReturn = strReturn & strTemp
    Next I
    HexToString = Right(strReturn, Len(strReturn) - 1) 'edit: removes first character (else printed value is empty)
    End Function
    
    Sub TranslateAllHex()
    
    Dim aCell As Range
    Dim aCellsValue As String
    Dim OutputString As String
    
    For Each aCell In Selection
        aCellsValue = aCell.Value
        If Not aCellsValue = "" Then
            If Left(aCellsValue, 2) = "0x" Then
                aCell.Value = HexToString(aCell.Value)
            End If
        End If
    Next aCell
    
    End Sub

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,914

    Re: Hex and String Conversions

    Since this old thread has been resurrected, let's note that all of the above make the assumption that the hex is representing an ANSI string, and not a Unicode string. A character in a Unicode string would take 4 hex characters to be represented (not 2). The Chr$(...) will convert to Unicode, but the incoming hex string must represent ANSI.

    Also, the StringToHex function will return gibberish if the incoming string contains any true Unicode characters (ASC(char) > 255).

    To be done correctly, one might consider an optional argument that specifies ANSI or Unicode. Also, an "ASC(char) AND 255" might be warranted to keep StringToHex from returning gibberish if ANSI is specified.

    Also, this could all be much better done with a byte array, which would eliminate so much thrashing around with temp strings and concatenations.

    Best Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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