Tags: appreciated, ascii, banary, base, binary, class, convert, hank, interger, library, net, string
Convert Data to Binary
On .Net » .Net Base Class Library
7,331 words with 6 Comments; publish: Wed, 19 Dec 2007 08:07:00 GMT; (10062.50, « »)
Hi All,
How to convert from any data, such as string, interger ar Ascii to banary? Any help would be appreciated.
Hank
http://class-library.itags.org/q_base-class-library_34488.html
All Comments
Leave a comment...
- 6 Comments

You can't. binary is a number system, any integer can be rendered as binary, but it can't be *converted* to binary, that makes no sense
Perhaps you need to explain what you want to do here ?
#1; Sun, 09 Sep 2007 02:12:00 GMT

- Thanks. I want to convert a string or a interger into a binary number to do bit operate #2; Sun, 09 Sep 2007 02:13:00 GMT

- You can use Convert.ToInt32 to convert a string to int (put the conversion in a try block if you think it's error prone) and then use bitwise operators on the int.#3; Sun, 09 Sep 2007 02:14:00 GMT

- Also have a look at the new Int32.TryParse method if you want parse strings without needing to catch an exception for invalid input. #4; Sun, 09 Sep 2007 02:15:00 GMT

An integer if also a binary/hex number, the only difference is the way that it is printed on the screen.
Sub Main() Dim n As Integern = 0
n = n
Or 1n = n
Or 8Console.WriteLine(ConvertToBinary(n))
n = n
Xor 32Console.WriteLine(ConvertToBinary(n))
n = n
And 32Console.WriteLine(ConvertToBinary(n))
n =
Not nConsole.WriteLine(ConvertToBinary(n))
Console.ReadLine()
End Sub Public Function ConvertToBinary(ByVal num As Integer) As String Dim p, b As Integer Dim result As Stringb = 1
result = ""
For p = 0 To 8 If (num And b) Thenresult = "1" & result
Elseresult = "0" & result
End Ifb *= 2
Next Return result End FunctionThe function ConvertToBinary only put each bit in the number into a readable string for the user to see and all binary operation are executed directly on the integer.
#5; Sun, 09 Sep 2007 02:16:00 GMT

- You can do the converToBinary thing with:
Convert.ToString(someint, 2)
If you have some bits set in a value and you need to read/set/unset the bits then just use logic. You could use the bitarray class I suppose...
Dim someint As Integer = 509385034
Dim someintBinaryRepresentation As String = Convert.ToString(someint, 2)
Debug.WriteLine(someintBinaryRepresentation)
' which is :
' 00011110 01011100 10011001 01001010 say we wat to change these bits:
' 1 0 1 1
' then we can use the bitarray class
' there are several overloads of the constructor. We have to pass it an int array
Dim someintAsArray() As Integer = {someint}
Dim ba As New BitArray(someintAsArray)
' so set bis 0, 2, 16 to 1, and bit 12 to 0
ba(0) = True
ba(2) = True
ba(16) = True
ba(12) = False
' and see what we got:
ba.CopyTo(someintAsArray, 0)
Debug.WriteLine(Convert.ToString(someintAsArray(0), 2))
' before: 00011110 01011100 10011001 01001010
' after: 00011110 01011101 10001001 01001111
' Or you can just use logic
someint = 509385034 ' reset it
' set bit at 0
someint = someint Or (2 ^ 0)
' set bit at 2
someint = someint Or (2 ^ 2)
' set bit at 16
someint = someint Or (2 ^ 16)
' unset bit at 12
someint = someint And (Not (2 ^ 12))
Debug.WriteLine(Convert.ToString(someint, 2)) ' same as above#6; Sun, 09 Sep 2007 02:17:00 GMT