I'd like to convert a long to a hex string, basically just a six digit color code. Someone have the code on this?
Printable View
I'd like to convert a long to a hex string, basically just a six digit color code. Someone have the code on this?
Try this:
VB Code:
Function LongToHex(ByVal lValue As Long) As String LongToHex = "&H" & Hex(lValue) End Function
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?
In that case you can modify function as follows:
VB Code:
Function LongToHex(ByVal lValue As Long) As String Dim strHex As String strHex = Hex(lValue) If Len(strHex) < 6 Then strHex = String(6 - Len(strHex), "0") & strHex End If strHex = "&H" & strHex LongToHex = strHex End Function
Ok, thanks.