|
-
Nov 16th, 2009, 03:47 AM
#1
Thread Starter
Hyperactive Member
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?
-
Nov 16th, 2009, 04:05 AM
#2
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 
-
Nov 16th, 2009, 04:07 AM
#3
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);
-
Nov 16th, 2009, 04:10 AM
#4
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 
-
Nov 16th, 2009, 04:13 AM
#5
Thread Starter
Hyperactive Member
Re: Base 2 String to Int64
 Originally Posted by Lightning
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...
 Originally Posted by motil
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|