Solved

converting hex value to decimal value

Posted on 1997-06-25
4
363 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 42
Windows Script Host failed (Access is denied.) error 6 146
VB6 - Compare and highlight cell not the same 3 51
TT Auto Dashboard 13 92
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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 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