Solved

converting hex value to decimal value

Posted on 1997-06-25
4
357 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
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
Johanl earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now