Solved

Conversion Routines

Posted on 2001-08-27
6
224 Views
Last Modified: 2012-06-27
Hi all,

Does anybody out there have conversion routines for the following.

Decimal to Binary
Binary to Decimal

Vin.  
0
Comment
Question by:VincentLawlor
[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
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
ture earned 50 total points
ID: 6427964
Vin,

I'm sure that there must be better solutions around, but I post this one anyway.

Function DecToBin(d As Long) As String
  Dim x As Integer
  Dim b As String
 
  Do
    If d And 2 ^ x Then
      b = "1" & b
    Else
      b = "0" & b
    End If
    x = x + 1
  Loop Until 2 ^ x > d

  DecToBin = b
End Function

Function BinToDec(b As String) As Long
  Dim x As Integer
  Dim d As Long
 
  For x = 1 To Len(b)
    If Mid(Right(b, x), 1, 1) = "1" Then
      d = d + 2 ^ (x - 1)
    End If
  Next x
 
  BinToDec = d
End Function

Ture Magnusson
Karlstad, Sweden
0
 
LVL 4

Author Comment

by:VincentLawlor
ID: 6427985
That will do the job

Just couldn't remember the twos compliment routines and I'm in a bit of a hurry.

Thanks.

Vin.
0
 
LVL 22

Expert Comment

by:ture
ID: 6428011
Thanks for the points, Vin! I'm glad that I could help you!

/Ture
0
Technology Partners: 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!

 
LVL 4

Author Comment

by:VincentLawlor
ID: 6428075
Don't suppose you have hex routines of a similar nature

The VB Hex function works fine but if I try to convert a Hex value

Function ConvertHexToDec(strHex as String) as Long

dim strValue as String
dim iTemp as Integer
strValue = "&H" + strHex

ConvertHexToDec = Val(strValue)

Exit Function

I get -1 not 65535 as expected for FFFF

Any ideas

Vin.




 
0
 
LVL 4

Author Comment

by:VincentLawlor
ID: 6428292
I have the Hex to decimal function

Function HexToDec(strInput As String) As Variant

    Dim iCounter As Integer
    iCounter = 0
    Dim lResult
    lResult = 0
    Dim radix As Integer
    radix = 16
    For iCounter = 1 To Len(strInput)
        lResult = lResult * radix
        If Asc(Mid(strInput, iCounter)) >= 48 And Asc(Mid(strInput, iCounter)) <= 57 Then
            lResult = lResult + (Asc(Mid(strInput, iCounter)) - 48)
        Else
            lResult = lResult + (Asc(UCase(Mid(strInput, iCounter))) - 65) + 10
        End If
       
    Next iCounter
         
    HexToDec = lResult

End Function

Do you by any chance have a DecToHex function the VB one only works up to ffffffff

Vin.
0
 
LVL 22

Expert Comment

by:ture
ID: 6428452
Vin,

There's really no need for a DecToHex function, because there is already a HEX function that converts a number to hex. In this code I've "encapsuled" it within a custom function named DecToHex.

Function DecToHex(d As Long) As String
  DecToHex = Hex(d)
End Function


It is also easy to convert hext to decimal. Just precede the hex number with "&H" and use VB's CDbl conversion function.

Function HexToDec(h As String) As Long
  HexToDec = CDbl("&H" & h)
End Function

/Ture
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 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…

728 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