How to Convert Bits to Bytes in VB.Net

How to Convert Bits to Bytes in VB.Net thumbnail
You can convert the bytes of a file to bits in VB.NET by using the Convert.ToString method.

There are many reasons why you might want to see the binary code of a file. You may be interested in making edits to it at the most basic level. Or you might want to process the binary string that composes the file through an ODBC data source. Whatever the case, converting the bytes of a given file into bits requires the creation of strings to display the values. You can perform this task with a few lines of code in VB.NET.

Instructions

    • 1

      Create a new VB.NET module in your preferred editor.

    • 2

      Insert the following code into your module:

      Dim fStrm As New FileStream("c:\test.gif", FileMode.Open, FileAccess.Read)

      Dim bytes(9) As Byte

      fStrm.Read(bytes, 0, bytes.Length)

      For i As Integer = 0 To bytes.Length - 1

      RichTextBox2.Text += Convert.ToString(bytes(i), 2).PadLeft(8, "0"c) & vbCrLf

      Next

      fStrm.Close()

      fStrm.Dispose()

      Substitute the file you are attempting to read for the instance of c:\test.gif above.

    • 3

      Save and compile your code. The module will read the file you specified and return its content to you in the format of binary bits.

Related Searches:

References

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

Related Ads

Featured