Results 1 to 5 of 5

Thread: Converting a Long to a Hex string.[resolved]

  1. #1

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Resolved Converting a Long to a Hex string.[resolved]

    I'd like to convert a long to a hex string, basically just a six digit color code. Someone have the code on this?
    Last edited by Nove; Jul 20th, 2005 at 09:28 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Converting a Long to a Hex string.

    Try this:
    VB Code:
    1. Function LongToHex(ByVal lValue As Long) As String
    2.     LongToHex = "&H" & Hex(lValue)
    3. End Function

  3. #3

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Converting a Long to a Hex string.

    That works fairly well, but if the number isn't big enough it's not going to fill six digits. For instance, pure white on there is going to just be "&H0". It's still Hex, but later I'm going to want to split the Hex into it's RGB components, which will require more code if it's not at the right length. Is there a better hex conversion, or even a good method of converting a long number into RGB?

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Converting a Long to a Hex string.

    In that case you can modify function as follows:
    VB Code:
    1. Function LongToHex(ByVal lValue As Long) As String
    2. Dim strHex As String
    3.  
    4.     strHex = Hex(lValue)
    5.     If Len(strHex) < 6 Then
    6.         strHex = String(6 - Len(strHex), "0") & strHex
    7.     End If
    8.     strHex = "&H" & strHex
    9.     LongToHex = strHex
    10.  
    11. End Function

  5. #5

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Converting a Long to a Hex string.[resolved]

    Ok, thanks.

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