Results 1 to 5 of 5

Thread: Base 2 String to Int64

  1. #1

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Base 2 String to Int64

    Hi all,

    There is a lot on the internet about converting a int to base 2 string, but I need to convert it back.

    So I have a string:

    "01010011100"
    Do something here
    answer: 668

    I need to get the number 668 somehow.

    Any ideas?

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Base 2 String to Int64

    here is something i found and changed a bit to fit your need:

    Code:
     Public Function Bin_To_Dec(ByVal Bin As String) As Double
            Dim dec As Double = Nothing
            Dim length As Integer = Len(Bin)
            Dim temp As Integer = Nothing
            Dim x As Integer = Nothing
            For x = 1 To length
                temp = CInt(Val(Mid(Bin, length, 1)))
                length = length - 1
                If temp <> 0 Then
                    dec += (2 ^ (x - 1))
                End If
            Next
    
            Return dec
        End Function
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Base 2 String to Int64

    This works (it is in C# but converting to vb.Net shouldn't be a prob)
    Code:
    string binary = "111";
    int base = 2; 
    int integer = Convert.ToInt32(binary, base);
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Base 2 String to Int64

    Lighting is right, here it is converted:

    Code:
     Dim binary As String = "01010011100"
     Dim base As Integer = 2
     Dim int As Integer = Convert.ToInt32(binary, base)
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Base 2 String to Int64

    Quote Originally Posted by Lightning View Post
    This works (it is in C# but converting to vb.Net shouldn't be a prob)
    Code:
    string binary = "111";
    int base = 2; 
    int integer = Convert.ToInt32(binary, base);
    Nice one thnx. Everytime I see a code in C#, I wonder why I ever started writing this program in vb.net lol. C# makes much more sense . Maybe that's because I am used to Java...


    Quote Originally Posted by motil View Post
    here is something i found and changed a bit to fit your need:

    Code:
     Public Function Bin_To_Dec(ByVal Bin As String) As Double
            Dim dec As Double = Nothing
            Dim length As Integer = Len(Bin)
            Dim temp As Integer = Nothing
            Dim x As Integer = Nothing
            For x = 1 To length
                temp = CInt(Val(Mid(Bin, length, 1)))
                length = length - 1
                If temp <> 0 Then
                    dec += (2 ^ (x - 1))
                End If
            Next
    
            Return dec
        End Function
    Thnx for your reply. I won't use it but it's nice to see the logic behind it. Thnx!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width