OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 18-07-2006, 16:28
ddonnahoe's Avatar
ddonnahoe ddonnahoe is offline
Senior Member
 
Join Date: Jan 2004
Location: Louisville, KY
Posts: 552
Send a message via ICQ to ddonnahoe Send a message via AIM to ddonnahoe Send a message via MSN to ddonnahoe Send a message via Yahoo to ddonnahoe
Default 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.
Reply With Quote
  #2  
Old 18-07-2006, 21:24
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

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.
Reply With Quote
Reply


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

Advanced Search
Display Modes

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 Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Feet Inches to Inches and Decimal Function shelbym CorelDRAW/Corel DESIGNER VBA 1 06-06-2005 13:01


All times are GMT -5. The time now is 20:47.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com