C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Converting from Binary to Integer and Integer to Binary


Posted Date: 06 Oct 2004    Resource Type: Articles    Category: .NET Framework
Author: Kamliesh NadarMember Level: Gold    
Rating: 1 out of 5Points: 3



Converting from Binary to Integer and Integer to Binary

ToInteger and ToBinary are two functions that accepts Binary and Integer as input parameters and returns respective output as Binary and Integer. The code doesn’t provide any validation for error trapping.

From - Binary to Integer

Private Function ToInteger(ByVal x As Long) As String
Dim temp As String
Dim ch As Char
Dim multiply As Integer = 1
Dim subtract As Integer = 1
Dim len As Integer = CStr(x).Length
For Each ch In CStr(x)
For len = 1 To CStr(x).Length - subtract
multiply = multiply * 2
Next
multiply = CInt(ch.ToString) * multiply
temp = multiply + temp
subtract = subtract + 1
multiply = 1
Next
Return temp
End Function


From - Integer to Binary

Private Function ToBinary(ByVal x As Long) As String
Dim temp As String = ""
Do
If x Mod 2 Then
temp = "1" + temp
Else
temp = "0" + temp
End If
x = x \ 2
If x < 1 Then Exit Do
Loop
Return temp
End Function

You can add this function to your application and call it with passing the respective parameter.




Responses

Author: Steve Howard    30 Oct 2004Member Level: Bronze   Points : 0
I modified these functions to correct method parameter and return types and also streamlined them a bit.

Public Function ToInteger(ByVal x As String) As Integer
Dim temp As Integer
Dim ch As Char
Dim multiply As Integer = 1
Dim subtract As Integer = 1
Dim len As Integer

For Each ch In x
For len = 1 To x.Length - subtract
multiply *= 2
Next
multiply *= Integer.Parse(ch.ToString)
temp += multiply
subtract += 1
multiply = 1
Next

Return temp
End Function

Public Function ToBinary(ByVal x As Integer) As String
Dim temp As String = ""

Do While x > 0
If x Mod 2 = 0 Then
temp = "0" + temp
Else
temp = "1" + temp
End If
x = x \ 2
Loop
Return temp
End Function

Hope they are ok now!

Cheers,
Steve


Author: J Ladd    12 Nov 2004Member Level: Bronze   Points : 0
I wanted the leading zeros in an 8-bit representaion.
I also used a string builder which is more efficient that using just a string.

Imports System.Text


Public Function ToBinary(ByVal x As Integer) As String

Dim Bin As StringBuilder = New StringBuilder

Do While x > 0
If x Mod 2 = 0 Then
Bin.Insert(0, "0")

Else
Bin.Insert(0, "1")

End If
x = x \ 2
Loop

Bin.Insert(0, "0", 8 - Bin.Length)
Return Bin.ToString

End Function


Author: Steve Howard    13 Nov 2004Member Level: Bronze   Points : 0
I just discovered the built in conversion function. The function could also be written:

Public Function ToBinary(Int as Integer) As String
Return Convert.ToString(Int, 2)
End Function

Where Int can be any integer value 16, 32 or 64 bit. The function's 2nd parameter designates the numeric base (radix) to be returned eg. base 2, 8 or 16.

Hope this helps.

Cheers,
Steve


Author: Steve Howard    14 Nov 2004Member Level: Bronze   Points : 0
And finally, to convert a binary string to an integer:

Public Function ToInteger(ByVal s as String) As Integer
Return Convert.ToInt32(s, 2)
End Function

Variations on this ToInt function exist for other integer types as well. You may now question the need for creating your own functions, but you could wrap the various conversions in your own class along with formatting features I guess.

Cheers,
Steve


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Exploit .NET Support in Yukon
Previous Resource: Delegate with a simple example
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use