x isn't defined as local so might cause a problem if you had a global variable called x.
How about a recursive definition?
If the binary number string to be converted is "" then
the value is 0
else
the value is 2 * binaryTohexadecimal(all but the last character of the string) + decimal value of the last character in the string
endif
Code:Function BinToHex(bits As String) As Long
If (bits = "") Then
BinToHex = 0
Else
BinToHex = 2 * BinToHex(Left(bits, Len(bits) - 1)) + CLng(Right(bits, 1))
End If
End Function
If you're prepared to accept that function return values in VB are automatically initialised to zero unless altered then
Code:Function BinToHex(bits As String) As Long
If (bits <> "") Then
BinToHex = 2 * BinToHex(Left(bits, Len(bits) - 1)) + CLng(Right(bits, 1))
End If
End Function
would work.
Let's hear it for recursion!