Function converting Binary numbers

S

Sasa

Hello,

I got this function converting binary numbers (string
form) into decimal numbers (integer form).
-----------------------------------------
Function Bin2Dec(Binar As String) As Integer

Dim i As Integer, n As Integer, D As String

For i = 1 To 15
If IsNumeric(i(Binar, i, 1)) Then
D = D + Mid(Binar, i, 1)
End If
Next
For i = 1 To 8
n = n + CInt(Mid(Binar, i, 1)) * 2 ^ (8 - i)
Next
Bin2Dec = n
End Function
-----------------------------------------

The problem is that function converts only the the numbers
8-digits long. I got also the binary numbers long 2 or 9
digits long. How should I update the code to have the
ability to convert the binary numbers with count of digits
less and more then 8.
Thanks.

Regards.
 
H

Helmut Weber

Hi Sasa,
only a minor mistake.
It is this line:
n = n + CInt(Mid(Binar, i, 1)) * 2 ^ (8 - i)
should be
n = n + CInt(Mid(D, i, 1)) * 2 ^ (8 - i)
instead.


Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
S

Sasa

Ok, thanks, Helmut.

Do you know the solution of my problem?
What can I do if I got the binary numbers more or less
longer then 8 digits? Because this code is converting the
8-digits numbers ONLY. If I try 4 or 10 digits long binary
number, word generates an error. How can I update the code?
Help appreciated.

Best regards.
 
H

Helmut Weber

Hi Sasa,

no problem. But I can't test it, as I've deleted
the code in the meantime. It is the "8" that limits
your function to a string of length 8. Better use
len(binar) and len(D) in the loops. And beware
of datatype integer for large numbers. Though any
type has its limits, type "long" lasts longer.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
P

Peter Hewett

Hi Sasa

Use this version, it will accept a binary number between 1 and 31 digits long.

Function Bin2DecL(ByVal strNumber As String) As Long
Dim lngNumber As Long
Dim ilngCharacter As Long
Dim lngCharacter As Long
Dim lngLength As Long

' Iterate the string to make sure its a binary number
lngLength = Len(strNumber)
If lngLength > 0 And lngLength <= 31 Then
For ilngCharacter = 1 To lngLength

' The character must be a zero or one
lngCharacter = Asc(Mid$(strNumber, ilngCharacter, 1))
If lngCharacter <> 48 And lngCharacter <> 49 Then

' No point in continuing as it's not a binary number
Err.Raise 5
End If
Next

' Convert binary number
For ilngCharacter = lngLength To 1 Step -1
lngNumber = lngNumber + CLng(Mid(strNumber, ilngCharacter, 1)) * 2 ^ _
(lngLength - ilngCharacter)
Next
End If
Bin2DecL = lngNumber
End Function

Bin2DecL("000001") = 1
Bin2DecL("000101") = 5
Bin2DecL("100101") = 37

If you want the 32 bit version you'll have to do that yourself!

PS. DOn't be afraid of using long (verbose) variable names. The only difference they
make to your code is that your code becomes readable!

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top