OberonPlace.com Forums > VBA Hex to Decimal
 Blogs Gallery FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

#1
18-07-2006, 16:28
 ddonnahoe Senior Member Join Date: Jan 2004 Location: Louisville, KY Posts: 552
Hex to Decimal

Alex, I know you are a busy guy, but is there a way to convert Hex code to Decimal in VBA. I am creating a HEX-RGB-CMYK generator that Sallybode wrote about sometime ago. I want to be able to have the txbHEXr_Change() event convert from the users input of hex code to Decimal in another textbox.

My current RGB to HEX sub lloks like this...
Code:
```Sub HexConverter()
'convert units to HEX
txbHEXr.Text = Format(Hex(r1), "00")
txbHEXg.Text = Format(Hex(g1), "00")
txbHEXb.Text = Format(Hex(b1), "00")
End Sub```
__________________
Sean
Waiting for a ride in the T.A.R.D.I.S.
#2
18-07-2006, 21:24
 Alex Administrator Join Date: Nov 2002 Posts: 1,940 Blog Entries: 4

That's easy. You need to use the Val function and prepend the hexadecimal value with &h like this:

Code:
```Dim sHex As String
Dim nDec As Long
sHex = "3FF"
nDec = Val("&h" & sHex)```
There is only one catch. When the hex value is in the range of 8000-FFFF you will get a negative value, even though you might expect a Long.

You can add this line to correct for that:

Code:
`If nDec < 0 And Len(sHex) < 5 Then nDec = nDec + 65536`
By the way, I don't think your expression Format(Hex(r1), "00") will give the correct results for 1 digit values in the range of 0A-0F. I would use is this instead:

Code:
`Right("00" & Hex(r1), 2)`
Or more generally:

Code:
```Function FormatHex(ByVal v As Long, ByVal nNumDigits As String) As String
FormatHex = Right(String(nNumDigits, "0") & Hex(v), nNumDigits)
End Function```
So you can use it like:

Code:
`txbHEXr.Text = FormatHex(r1, 2)`

Last edited by Alex; 18-07-2006 at 21:31.

 Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home OberonPlace Discussion     Site News     Web site suggestions     Image Gallery     Testing Oberon Products     CurveWorks     SecuriDesign     Calendar Wizard     Oberon Function Plotter     Jigsaw Puzzle Creator     Other Oberon Commercial Products     New product ideas Corel User Forums     CorelDRAW         General         Tutorials         FAQ         Feature requests/wishlist         Macros/Add-ons         Artwork Showcase     Corel DESIGNER         General         Tutorials         FAQ         Feature requests/wishlist         Macros/Add-ons         Artwork Showcase     Corel PHOTO-PAINT         General         Tutorials         FAQ         Feature requests/wishlist         Macros/Add-ons         Artwork Showcase     General         About Corel         Off-Topic Developer Forums     OberonPlace Development Portal     VBA         CorelDRAW/Corel DESIGNER VBA         Corel Photo-Paint VBA         Code Critique     Corel Script         CorelDRAW CS         Corel Photo-Paint CS