Results 1 to 9 of 9
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function strHexToChar(ByVal strText As Strin

    Here is a rather unwieldy way of converting a string of hexadecimal (base 16) digit characters to a character string.

    Since this is an internal function (called by other procedures, not directly called by the user) I am allowed to make the assumption that the incoming string of hex digits is a valid string; that is, that it is an even number of characters taken from the set 0-9 and A-F.

    Somewhere in between the user call (a Macro) and this procedure must be a procedure that checks that the string is valid.

    I have a related function lngHexToLong.

    <pre>Public Function strHexToChar(ByVal strText As String) As String
    ' Procedure : strHexToChar
    ' Description: Convert a string of hexadecimal digits to a string of characters.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING of characters in range 0-9 and A-F.
    ' Returns: STRING.
    ' Assumes: Pairs of valid Hex digits are presented.
    ' Side Effects: None.
    ' Tested: By the calls shown below.

    Dim intChar As Integer
    Dim strResult As String
    While Len(strText) > 0
    intChar = 0
    Select Case Left$(strText, 1)
    Case "0"
    intChar = intChar * 16 + 0
    Case "1"
    intChar = intChar * 16 + 1
    Case "2"
    intChar = intChar * 16 + 2
    Case "3"
    intChar = intChar * 16 + 3
    Case "4"
    intChar = intChar * 16 + 4
    Case "5"
    intChar = intChar * 16 + 5
    Case "6"
    intChar = intChar * 16 + 6
    Case "7"
    intChar = intChar * 16 + 7
    Case "8"
    intChar = intChar * 16 + 8
    Case "9"
    intChar = intChar * 16 + 9
    Case "A"
    intChar = intChar * 16 + 10
    Case "B"
    intChar = intChar * 16 + 11
    Case "C"
    intChar = intChar * 16 + 12
    Case "D"
    intChar = intChar * 16 + 13
    Case "E"
    intChar = intChar * 16 + 14
    Case "F"
    intChar = intChar * 16 + 15
    Case Else
    End Select
    strText = Right$(strText, Len(strText) - 1)

    Select Case Left$(strText, 1)
    Case "0"
    intChar = intChar * 16 + 0
    Case "1"
    intChar = intChar * 16 + 1
    Case "2"
    intChar = intChar * 16 + 2
    Case "3"
    intChar = intChar * 16 + 3
    Case "4"
    intChar = intChar * 16 + 4
    Case "5"
    intChar = intChar * 16 + 5
    Case "6"
    intChar = intChar * 16 + 6
    Case "7"
    intChar = intChar * 16 + 7
    Case "8"
    intChar = intChar * 16 + 8
    Case "9"
    intChar = intChar * 16 + 9
    Case "A"
    intChar = intChar * 16 + 10
    Case "B"
    intChar = intChar * 16 + 11
    Case "C"
    intChar = intChar * 16 + 12
    Case "D"
    intChar = intChar * 16 + 13
    Case "E"
    intChar = intChar * 16 + 14
    Case "F"
    intChar = intChar * 16 + 15
    Case Else
    End Select
    strText = Right$(strText, Len(strText) - 1)
    strResult = strResult & Chr$(intChar)
    Wend
    strHexToChar = strResult
    'Sub TESTstrHexToChar()
    'MsgBox strHexToChar("414243") ' ABC
    'End Sub
    End Function
    </pre>


  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    Chris: Here is a slightly smaller version of your routine:

    <pre>Public Function strHexToChar(ByVal strText As String) As String
    If Len(strText) Mod 2 <> 0 Then
    strText = "0" & strText
    End If
    Do While Len(strText) > 0
    strHexToChar = strHexToChar & Chr$(Val("&H" & Left$(strText, 2)))
    strText = Right$(strText, Len(strText) - 2)
    Loop
    End Function
    </pre>

    Legare Coleman

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    Thanks, Legare. I especially like your "Mod 2" touch. Bulelt-proof is good.

    I am waiting for the debate on "select case vs. looping". I noticed the start of thread on "select case" on my return to the lounge.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    Legare, I'd be interested in comments on my use of an interim local variable like "strResult".

    I've used languages where one cannot use the name of the function as a source of data within the function body, so that constructs of the form "strHexToChar = strHexToChar & ....." are not allowed. I've grown into the habit of using an intrerim local variable.

    I don't think it takes up any more memory (after all, strHexToChar = strHexToChar & ..." has to store an internmediate result somewhere) and the local variable method may be microsecondaly (?) faster, since we're not dealing with things like "function header blocks".

    Readability is probably the main issue. A local variable aids in debugging, tracing and so on.



    I have incorprated your method into "lngHexToLong", which should appear tomorrow.

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    Using an interim local variable does use slightly more memory since at the time you assign the interim to the function result they both have to exist. It is also ever so slightly slower since you have the extra code to assign the interim to the result.

    However, I tend to use an interim for a couple of reasons. First, I think it is more readable and understandable, second (as you said) I think it makes debugging a little easier, and third I don't have to deal with partial results in the function return if the code discovers an error part way through and has to abort the function. I also tend to assign arguments to internal variables rather than change the argument (as I did in the code I posted in this thread). This is because I have found bugs in some compilers where arguments that were supposed to be ByVal were actually ByRef, and I ended up modifying a constant value used in many places in the calling routine. That was a really nasty problem to find.
    Legare Coleman

  6. #6
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    Might be slightly quicker to loop like:

    For i = 1 To Len(HexString) Step 2
    NewString = NewString & Chr$(Val("&H" & Mid$(HexString,i,2)))
    Next i

    because you don't have to continually re-allocate the original string.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    Looks good. Thank you.

    Those of you who have read Weinberg's "Psychology of Computer programming" will recognise the sequence here. He uses an example of one examiner finding bugs in code, and the code-author reasoning that if he had overlooked six bugs, the first examiner might have overlooked some others, and so takes the code to a second examiner.


    In my case, I don't have bugs (often!) but I *do* suffer from a sever lack of knowedge of the tools available to me in VBA.

    I've got good ideas (often!) but my implementaion tend smore towards demonstrating that the idea has potential, rather than the fine points of coding.

    By posting my code here, just LOOK at the improvements ...

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    >Weinberg's "Psychology of Computer programming"

    Chris, I think you might be showing your age now! (I remember that from the days of punch cards and daily runs- probably about 1975?)
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strHexToChar(ByVal strText As Strin

    "Showing" my age? Everything about me is eroding, leaving just a column of years .....

    His book, as you will know, is a pretty good read at any time. I work my way through it about once every five years or so. His examples are, I suspect, faked up, but they do mimic work patterns that I've observed.


    Just look at how fequently I write a basic function because I assume I have to. That's my FORTRAN II and assembly-language roots showing. Nowadays, especially in Micro computers, economies of population (millions on VBA programmers) makes it feasibl;e to embed useful functions.

    I still assume that I have to do it the old way, my way, with a funcion that probably doesn't work as well as the in-built function. Just like the guy who wrote his own I-O routines ...

Posting Permissions

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