Solved

converting hex value to decimal value

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now