Answers for "excelvba binary bits to short integer"

VBA
20

excelvba binary bits to short integer

'Extremely fast VBA function to convert a binary string to a 16-bit Integer:

Function BitsToInteger%(bits$)
    Dim i&
    Static b() As Byte
    If LenB(bits) > 32 Then Exit Function
    If LenB(bits) = 32 Then
        b = bits
    Else
        b = String$(16 - Len(bits), "0") & bits
    End If
    For i = 2 To 30 Step 2
        BitsToInteger = 2 * BitsToInteger Or (b(i) Xor 48)
    Next
    If (b(0) Xor 48) Then BitsToInteger = BitsToInteger Or &H8000
End Function

'Example:

MsgBox BitsToInteger("1111111111111111")    '<--displays: -1
MsgBox BitsToInteger("0111111111111111")    '<--displays:  32767
Posted by: Guest on February-04-2021

Code answers related to "excelvba binary bits to short integer"

Code answers related to "VBA"

Browse Popular Code Answers by Language