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?
Printable View
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?
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
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);
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)
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...
Thnx for your reply. I won't use it but it's nice to see the logic behind it. Thnx!