Stuck trying to convert a string to decimal

Vitesse

Member
Joined
Aug 1, 2009
Messages
5
Location
Essex
Programming Experience
Beginner
Hi guys

i'm a little stuck with a personal project, i'm interfacing a MiniBee usb output card to my pc and having a few code problems, i am trying to control outputs 1 to 8 which requires a decimal number that relates to the binary pattern of the ouputs if that makes sense i.e

1 = output 1 on
2 = output 2 on
3= output 1 and 2 on
etc etc


So, i figured the easiest way to individually control outputs was to use public boolean vars Out1 to Out8 so i can use those anywhere in the code and use a form timer to trigger the updating of the output

So the bit im stuck on is i've done a test using 8 checkboxes to set Out 1-8 as true/false

then i've used this code:

VB.NET:
    Private Sub DoOutputs()

        Dim String1, String2, String3, String4, String5, String6, String7, String8 As String

        Dim OutVal As Integer

        If Out1 = True Then String1 = "1" Else String1 = "0"
        If Out2 = True Then String2 = "1" Else String2 = "0"
        If Out3 = True Then String3 = "1" Else String3 = "0"
        If Out4 = True Then String4 = "1" Else String4 = "0"
        If Out5 = True Then String5 = "1" Else String5 = "0"
        If Out6 = True Then String6 = "1" Else String6 = "0"
        If Out7 = True Then String7 = "1" Else String7 = "0"
        If Out8 = True Then String8 = "1" Else String8 = "0"



        TempOutput = ""
        TempOutput = String.Concat(String8, String7, String6, String5, String4, String3, String2, String1)

        OutVal = Convert.ToInt32(TempOutput)

        SetOutputs(OutVal) 'set Minibee outputs


    End Sub

It nearly works but i cant figure out why when i set Out2 true or Out4 true that it actually causes Output 4 on the MiniBee to turn on, also output6 is ON when a Out4 or out6 is true everything else works fine apart from that, any ideas ???


Thanks
 
Last edited:
i've solved it now, was looking at it in too complicated a way i think

Just add decimal values up that represent the binary like this:
VB.NET:
Dim OutputInteger as Integer
OutputInteger = 0

        If Out1 = True Then OutputInteger = OutputInteger + 1
        If Out2 = True Then OutputInteger = OutputInteger + 2
        If Out3 = True Then OutputInteger = OutputInteger + 4
        If Out4 = True Then OutputInteger = OutputInteger + 8
        If Out5 = True Then OutputInteger = OutputInteger + 16
        If Out6 = True Then OutputInteger = OutputInteger + 32
        If Out7 = True Then OutputInteger = OutputInteger + 64
        If Out8 = True Then OutputInteger = OutputInteger + 128

SetOutputs(OutputInteger)
 
What I meant was that when you do your Convert.ToInt32 function, you forgot to tell it that you were working in binary. So when you asked it to Convert.ToInt32 ("10"), it thought you meant ten rather than two. So what you actually got was 10 base 10 = 1010 base 2 - which is why bit 4 and bit 2 were both coming on when you asked for bit 2.

In your original code, all you needed to change was

VB.NET:
OutVal = Convert.ToInt32(TempOutput, 2)

Note - most PLCs use 0 to 7 as bits, and not 1 to 8.
 
Thanks InertiaM

That makes sense now as to why that was happening, since i'm very new to .net is there a more compact way to write the code i've done?

I know its weird to use 1-8 as opposed to 0-16 but I figured it would actually just make it easier to remember whats-what as it has 14 outputs although im only using 8.

Cheers
 
Vitesse said:
since i'm very new to .net is there a more compact way to write the code i've done?
VB.NET:
If Out1 = True Then OutputInteger = OutputInteger + 1
VB.NET:
If Out1 Then OutputInteger += 1
You can also define a flags Enum to be operated with bitwise operations:
VB.NET:
<Flags()> Public Enum Outputs
    Out1 = 1
    Out2 = 2
    Out3 = 4
    'etc
End Enum

'usage sample
    Dim out As Outputs = Outputs.Out1 Or Outputs.Out3
    Dim outint As Integer = CInt(out)
 
Back
Top