Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

converting hex value to decimal value

Posted on 1997-06-25
4
Medium Priority
?
401 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

885 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