Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting hex value to decimal value

Posted on 1997-06-25
4
Medium Priority
?
395 Views
Last Modified: 2006-11-17
i have a problem, in my app. i have to convert decimal to hex (no problem with hex(variable)) but i also want to do the opposite. I want to convert a hex value into a decimal value and i have not found any way to do it... help me..

p.s: i use vb-pro 3
0
Comment
Question by:njacques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
Johanl earned 100 total points
ID: 1427720
Hi Jacques,

Create a module file, insert two functions :

Public Function HexToDec(n)
Dim nLen
Dim i
Dim ndec

ndec = 0
nLen = Len(n)

For i = nLen - 1 To 0 Step -1
   
    ndec = ndec + (16 ^ i) * GetDecValue(UCase(Mid(n, nLen - i, 1)))
Next
HexToDec = ndec
End Function


Public Function GetDecValue(n)
Dim RetValue

Select Case n
    Case "F"
        RetValue = 15
    Case "E"
        RetValue = 14
    Case "D"
        RetValue = 13
    Case "C"
        RetValue = 12
    Case "B"
        RetValue = 11
    Case "A"
        RetValue = 10
    Case Else
        RetValue = n
End Select
GetDecValue = RetValue
End Function

You can use the converting like :

idec=hextodec("FFFF")
msgbox idec

Regards
Johan Loos
jloos@glo.be

0
 

Author Comment

by:njacques
ID: 1427721
I cannot get you function to reply... I have placed it inside a module file (module1.bas) and the two function are contained into the pull down menu. so here is a part of my code..

If Len(text4.Text) < 6 Then
MsgBox ("Please enter a 6 digit hexadecimal number (RRGGBB)")
Exit Sub
End If

Dim rouge As String
Dim vert As String
Dim bleu As String
                 
rouge = UCase$(Left(text4.Text, 2))
vert = UCase$(Right(Left(text4.Text, 4), 2))
bleu = UCase$(Right(text4.Text, 2))

rougedec = HexToDec("rouge")

vertdec = HexToDec(vert)

bleudec = HexToDec(bleu)

label1.Caption = rougedec
label2.Caption = vertdec
label3.Caption = bleudec

hscroll1.Value = Val(rougedec)
hscroll2.Value = Val(vertdec)
hscroll3.Value = Val(bleudec)

the user enters a 6 digit string and the app split the rr-gg-bb so that they can be converted separatly. when the app get to you function call, the labels get blank... help me (am i using your function right) could my code cause this?

help me
0
 

Expert Comment

by:Johanl
ID: 1427722
rougedec=hextodec(rouge) and NOT rougedec=hextodec("rouge")

I've copied your code for testing and works fine.

Let know if it works now
Johan
0
 

Author Comment

by:njacques
ID: 1427723
i found out that val(&hff)
will do the job so o only got to do this

toconvert$ = "&h" + rouge
decimal = val(toconvert$)

and it converts, save a lot of time and coding

thanks anyway

bye

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question