Hex To Dec

rockies1

King Cobra
Local time
Today, 04:17
Joined
May 21, 2001
Messages
38
I have a Hex value (will be like "FEFF4CD8") I need to convert to Decimal, but I do not know how...

Anyone have any pointers?

Thanks!
 
from neatcode97...

Function Hex2Dec(strValue As String) As Long
On Error GoTo CnvrtErr
'
' Converts a string of hexadecimal digits into a decimal number.
' Valid input range '0' to '7FFFFFFF'
'
' Check to see if string already begins with &H.
If Left(strValue, 2) <> "&H" Then strValue = "&h" & strValue

' Check to see if string contains Decimals and strip them out.
If InStr(1, strValue, ".") Then strValue = Left(strValue, (InStr(1, strValue, ".") - 1))

Hex2Dec = CLng(strValue)
Exit Function

CnvrtErr:
Hex2Dec = 0

End Function

hth,
al
 
A bit old post, but had to do the same and the above does not seem to work properly.
To go from '#00FF00' to Long ('65280') , this will do the trick:
Code:
Private Function HEXtoRGB(strHexVal As String) As Long
'function converts HEX value to RGB value
On Error GoTo ErrHndlr
    Dim strR As String
    Dim lngR As Long
    Dim strG As String
    Dim lngG As Long
    Dim strB As String
    Dim lngB As Long
    
    'remove hash
    If Left(strHexVal, 1) = "#" Then
        strHexVal = Mid(strHexVal, 2, 6)
    Else
        'do nothing
    End If
    
    'cut HEX value into 3 parts R/G/B
    strR = "&H" & Left(strHexVal, 2)
    strG = "&H" & Mid(strHexVal, 3, 2)
    strB = "&H" & Right(strHexVal, 2)
    
    'convert each to Long
    lngR = CLng(strR)
    lngB = CLng(strB)
    lngG = CLng(strG)
    
    'get final value
    HEXtoRGB = RGB(lngR, lngG, lngB)

ExitHndlr:
    Exit Function

ErrHndlr:
    HEXtoLong = 0
    GoTo ExitHndlr
End Function

edit: as theDBguy suggested function name changed from HexToLong to HexToRGB.
 
Last edited:
Hi. The name of that function is a bit misleading. The real equivalent of Hex 00FF00 in Long Integer is -256. That function assumes you're trying to convert a Hex value into RGB value, which is not the same as the function name implies "HexToLong." Maybe it should be named "HexToRGB" instead. Just my 2 cents...
 
An oldie for sure.
Does that give the correct values? From what I've read, the Windows (and maybe other tools) color picker values are backwards for red and blue. This function doesn't seem to reverse them (BGR) as I've seen done elsewhere.
 
I know what you mean. I've seen it elsewhere, not sure where. But this works as it supposed to. I made something similar in Excel, as UserForm object colours cannot be set neither to HEX nor RGB value.
In access I was not able to set the rectangle background colour directly from the recordset, not sure why. Not always though, failed 1 in 10. This seems to do the trick.
 
If you were working (looping) with a DAO or ADO recordset and were trying to modify control colours accordingly, I would say that a) the rs loop would be too fast for Access to redraw the screen, and b) possibly you didn't think to Repaint the form. That would be where conditional formatting would likely perform better.
 
An oldie for sure.
Does that give the correct values? From what I've read, the Windows (and maybe other tools) color picker values are backwards for red and blue. This function doesn't seem to reverse them (BGR) as I've seen done elsewhere.

I've never heard that before. Any supporting evidence?
I have a colour converter that allow you to select colours and view their RGB, HEX and OLE values. http://www.mendipdatasystems.co.uk/colour-converter/4594450413
That app includes several converters though none were written by me.

From all I can see that assertion about the values being swopped seems to not be the case.
 
You could try something like this:

Code:
debug.Print clng( "&HFFEF4CD8" )
-1094440

Generate the hex string with the "&H" prefix, then pass it AS A STRING to the CLng function.
 
From what I've read, the Windows (and maybe other tools) color picker values are backwards for red and blue.

My experience has been that if you use Excel's RGB function, R & B are backwards vs. the order Access uses. I don't know if Access has a different RGB function, but if it does, I know it can't have the same function name without causing a potload of confusion. (You can figure out on your own what kind of potload I was describing...)
 
That comes from here
https://access-programmers.co.uk/forums/showthread.php?t=267808

and trying the function out in code of my own. I had to reverse the converted values. I don't know if I can find that now because I use a db that has a lot of stuff in it because it's a conglomeration of lots of forum problems. Plus I don't recall where I got the initial values from and I think the point of the link is that it matters. See post 4 and 11, but don't just focus on those. Apparently OP has run across this so I'm not alone. Maybe Google hex to rgb conversion and see if you spot anything because I will be tied up with visitors tonight.
 

Users who are viewing this thread

Back
Top Bottom