Jump to content

Binary string and integers in Visual Basic Express 2010

How do I make it so I can take a binary number up to 8 bits in a string "01010101" and then make it so they are in an array of Integers?

 

I will example by using letters.

String = "abcdefgh"

Binary(1) = a

Binary(2) = b

Binary(3) = c

Binary(4) = d

Binary(5) = e

Binary(6) = f

Binary(7) = g

Binary(8) = h

 

Please help, I couldn't find it on the internet and I need it for my Computing GCSE oursework 

 

 

Link to post
Share on other sites

Use a for loop to loop through the integers in the string, here's a java example I sorta forget how to format vb.net so I'm not even going to try:

 

for (int i = 0; i = listOfDigits.length; i++){

    if (listOfDigits.getChar(i)) = "1" {

       NewList.set(i, "a")

      // repeat above block for 1 - 8

   }

}

 

That's insanely rough and the syntax is all for Java but you get the idea, basically loop through all characters in the string, check if they're 1 - 8 then assign their letter to the new array list.

Me: Yeah I just really can't get my H220 working again, I've tried everything that was suggested in the forum.

Brian (Swiftech Support): Hmm, have you tried slapping it?

Link to post
Share on other sites


Module Module1

Sub Main()

Dim someString = "01010101"

Dim someArray(someString.Length - 1) As Integer

For i As Integer = 0 To someArray.Length - 1

someArray(i) = Convert.ToInt32(someString(i).ToString)

Next

For Each i As Integer In someArray

Console.WriteLine(i)

Next

Console.ReadKey()

End Sub

End Module

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

I love how they are still teaching Visual Basic like it's got any current real world value!

 

C#, we can do it in one line!

using System;using System.Globalization;using System.Linq;namespace ConsoleApplication2{    static class Program    {        static void Main(string[] args)        {            const string SomeString = "01010101";            var someArray = SomeString.Select(c => Convert.ToInt32(c.ToString(CultureInfo.InvariantCulture))).ToArray();            Array.ForEach(someArray, Console.WriteLine);            Console.ReadKey();        }    }}

Alas, I digress :P

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Module Module1    Sub Main()        Dim someString = "01010101"        Dim someArray(someString.Length - 1) As Integer        For i As Integer = 0 To someArray.Length - 1            someArray(i) = Convert.ToInt32(someString(i).ToString)        Next        For Each i As Integer In someArray            Console.WriteLine(i)        Next        Console.ReadKey()    End SubEnd Module

Does this work in the Windows Form Application or does it work in Console Application?

 

 

 

I love how they are still teaching Visual Basic like it's got any current real world value!

 

C#, we can do it in one line!

using System;using System.Globalization;using System.Linq;namespace ConsoleApplication2{    static class Program    {        static void Main(string[] args)        {            const string SomeString = "01010101";            var someArray = SomeString.Select(c => Convert.ToInt32(c.ToString(CultureInfo.InvariantCulture))).ToArray();            Array.ForEach(someArray, Console.WriteLine);            Console.ReadKey();        }    }}

Alas, I digress :P

Oh well, VB is over soon, we starting Java and HTML in the new annual year (September) and hopefully C++ or C# in A Levels. But that's in just over 1 year away(I didn't realise how close it was before this sentence) :D

Link to post
Share on other sites

Does this work in the Windows Form Application or does it work in Console Application?

I just knocked it up as a console application. Lines #4 - #9 are what you're interested in really.

Oh well, VB is over soon, we starting Java and HTML in the new annual year (September) and hopefully C++ or C# in A Levels. But that's in just over 1 year away(I didn't realise how close it was before this sentence) :D

Very nice, those will defiantly serve you much better.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

I just knocked it up as a console application. Lines #4 - #9 are what your interested in really.

 

 
 

 

Very nice, those will defiantly serve you much better.

Yea, this year's work was to get us into knowing computing and learning the basics, next year will be good! :D

Link to post
Share on other sites

Dim someArray = someString.Select(Function(x) Integer.Parse(x)).ToArray

 

or

 

Dim someArray = Array.ConvertAll(someString.ToArray, AddressOf Integer.Parse)

 

or like these

 

Private Function Str2IntArray(ByVal inStr As String) As Integer()
    Str2IntArray = inStr.Select(Function(x) Integer.Parse(x)).ToArray
End Function

Private Function Str2IntArray(ByVal inStr As String) As Integer()
    Str2IntArray = Array.ConvertAll(inStr.ToArray, AddressOf Integer.Parse)
End Function

 

first one is kinda like what was present above, but I like the second one as its more clear by reading what it does and slightly faster.

Link to post
Share on other sites

Dim someArray = someString.Select(Function(x) Integer.Parse(x)).ToArray

 

or

 

Dim someArray = Array.ConvertAll(someString.ToArray, AddressOf Integer.Parse)

 

or like these

 

Private Function Str2IntArray(ByVal inStr As String) As Integer()

    Str2IntArray = inStr.Select(Function(x) Integer.Parse(x)).ToArray

End Function

Private Function Str2IntArray(ByVal inStr As String) As Integer()

    Str2IntArray = Array.ConvertAll(inStr.ToArray, AddressOf Integer.Parse)

End Function

 

first one is kinda like what was present above, but I like the second one as its more clear by reading what it does and slightly faster.

 

You're right, much faster and more concise.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×