0

I have found out how to do the opposite of converting a integer to a 16 bit Boolean array by this.

 Dim ND1_Array As New System.Collections.BitArray(System.BitConverter.GetBytes(Data(2)))

I have tried this

Dim barray(15) As Boolean : barray(0) = True : barray(2) = True : barray(4) = True
    Dim bittoint As Integer
    bittoint = Convert.ToInt32(barray(0), 0)

This code throws an error so ? I have looked all over the net but can't find how to do this.

bittoint = BitConverter.ToInt32(barray(0), 0)

This has an error as well Error 1 Value of type 'Boolean' cannot be converted to '1-dimensional array of Byte'. C:\PLC\TCPClientClean\TCPClientClean\ChatClient.vb 201 41 TCPClientClean

This is what I came up with. Not sure what is the better method? I just saw the new updated code that was provided.

Dim BoolStg As String
    Dim BoolArra1DexBit As Boolean
    Dim BitArray1ToInt16 As Integer
    For BarryDex = 0 To 15
        BoolArra1DexBit = BoolAray1(BarryDex)
        If BoolArra1DexBit = True Then
            BoolStg = "1" & BoolStg
        Else : BoolStg = "0" & BoolStg
        End If
    Next
    BitArray1ToInt16 = Convert.ToInt16(BoolStg, 2)
11
  • a more detailed error description please. Commented Feb 8, 2015 at 20:16
  • Invalid base and this in the immediate window A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll Commented Feb 8, 2015 at 20:20
  • you feed toint32 with a boolean, there is no second parameter here. Commented Feb 8, 2015 at 20:20
  • Try BitConverter.ToInt16/32 instead. Commented Feb 8, 2015 at 20:21
  • Oh, and you need to set option strict on. Commented Feb 8, 2015 at 20:29

2 Answers 2

1

little example

    Dim myFlags As Integer = &H11
    MsgBox(Convert.ToString(myFlags, 2))

    Dim ND1_Array As New System.Collections.BitArray(System.BitConverter.GetBytes(myFlags))

    Dim myFlags_tmparray(0) As Integer
    ND1_Array.CopyTo(myFlags_tmparray, 0)

    MsgBox(Convert.ToString(myFlags_tmparray(0), 2))
Sign up to request clarification or add additional context in comments.

Comments

0

Here's an extension method that will return the integer equivalent as little endian or big endian, and allows you to toggle TwosComplement as well:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim barray(15) As Boolean : barray(0) = True : barray(2) = True : barray(4) = True

        Dim BigEndianInteger As Integer = barray.ToInteger(False)
        Dim LittleEndianInteger As Integer = barray.ToInteger() ' <-- default is Little Endian with TwosComplement turned On

        Debug.Print("BigEndianInteger = " & BigEndianInteger)
        Debug.Print("LittleEndianInteger = " & LittleEndianInteger)
    End Sub

End Class

Public Module Extensions

    <Runtime.CompilerServices.Extension()> _
    Public Function ToInteger(ByVal BooleanArray() As Boolean, Optional ByVal LittleEndian As Boolean = True, Optional ByVal TwosComplement As Boolean = True) As Integer
        If BooleanArray.Length <= 32 Then
            Dim sum As Integer
            Dim values As New List(Of Boolean)(BooleanArray)
            If Not LittleEndian Then
                values.Reverse()
            End If
            For i As Integer = 0 To values.Count - 1
                If values(i) Then
                    If i < (values.Count - 1) Then
                        sum = sum + Math.Pow(2, i)
                    ElseIf TwosComplement Then
                        sum = sum - Math.Pow(2, i)
                    Else
                        sum = sum + Math.Pow(2, i)
                    End If
                End If
            Next
            Return sum
        Else
            Throw New Exception("Boolean array length must be less than or equal to 32.")
        End If
    End Function

End Module

8 Comments

It was Little endian and your code worked very well !! Thanks
This code that you provided @Idle_Mind works well except for when bit 15 of the array is true. For example Barray1 = 100000000000000
This code that you provided @Idle_Mind works well except for when bit 15 of the array is true. For example Barray1 = 1000000000000000 LittleEndian = 32768 ? It should be -32768 Barray1 = 1111111111111111 LittleEndian = 65535 ? It should be -1 by any binary to int conversion?
Ah...you never specified signed or unsigned. What signed number representation are you using here?
I am only about 6 months into learning vb. I normal write code for PLC's Programmable Logic Controllers. When those controllers convert it is just has a drop down list that will change from 16 bit binary, octal, decimal, Hex/BCD, ASCII. To me it is just a standard conversion that I am used to.. I just use a binary to integer calculator to find a conversion. I hope that is enough information to explain witch singed number representation.. I just need the conversion like a standard binary calculator would convert to integer/ decimal value. Hope this helps. Thx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.