Solved

# Conversion Routines

Posted on 2001-08-27
221 Views
Hi all,

Does anybody out there have conversion routines for the following.

Decimal to Binary
Binary to Decimal

Vin.
0
Question by:VincentLawlor
• 3
• 3

LVL 22

Accepted Solution

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
0

LVL 4

Author Comment

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

ID: 6428011

/Ture
0

LVL 4

Author Comment

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

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
For iCounter = 1 To Len(strInput)
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

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

Question has a verified solution.

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

### Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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.
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…
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…