Results 1 to 7 of 7

Thread: [RESOLVED] Convert Bytes into bits

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Resolved [RESOLVED] Convert Bytes into bits

    Hi all, I am trying to show / see the binary code of a file. So far I have this:
    VB Code:
    1. Dim fStrm As FileStream
    2.         Dim bytes(9) As Byte
    3.  
    4.  
    5.         fStrm = New FileStream("c:\test.gif", FileMode.Open, FileAccess.Read)
    6.         fStrm.Read(bytes, 0, bytes.Length)
    7.  
    8.         Dim i As Integer
    9.         For i = 0 To bytes.Length - 1
    10.             RichTextBox2.Text += bytes(i).ToString & vbCrLf
    11.         Next
    This gives me
    71
    73
    70
    56
    55
    97
    253
    0
    77
    0
    which I suppose are bytes. What is the logic interpreting the bytes as a range 0 through 255? How to make them 000101010s etc?
    VB 2005, Win Xp Pro sp2

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Convert Bytes into bits

    For each of those elements, run it through a binary to string function. Then concatenate all the strings, and you have your final binary.

    VB Code:
    1. private string GetBinaryString(Int32 intNumber)
    2.  
    3.         {
    4.             Int32 intBinary;
    5.             string strReturn = "";
    6.             while (intNumber > 0)
    7.             {
    8.                 intBinary = intNumber % 2; //get remainder
    9.                 strReturn += intBinary;
    10.                 intNumber = intNumber / 2;  //k
    11.             }
    12.  
    13.             char[] arrBinary;
    14.             arrBinary = strReturn.ToCharArray();
    15.             Array.Reverse(arrBinary);
    16.             strReturn = new string(arrBinary);
    17.             return strReturn;
    18.         }

  3. #3
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Convert Bytes into bits

    or...
    VB Code:
    1. Dim b As Byte = 100
    2.         Dim binaryString As String = Convert.ToString(b, 2).PadLeft(8, "0"c)

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Convert Bytes into bits



    D'oh.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Convert Bytes into bits

    Why would a bit need to be rounded from bytes? How can whole numbers produce decimals by multiplying it by 8? Why being "represented" at all and not simply given in it's natural form?

    I found many binary editors that do it in hex, maybe I should search for a hex editor to find one doing it in bin

    I though all it is about are 1s and 0s, after all a magnet has only 2 poles and yet can't find anything to show me those 1s & 0s in that something.dll in notepad.
    VB 2005, Win Xp Pro sp2

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Convert Bytes into bits

    You are reading the values from a file into an array of variables.
    The byte refers to the number of bits that are used to store the value. When you want to write this value out, you are not "converting Bytes to Bits", you are simply expressing the value.
    You have to create strings to display the values stored in these variables. The default behaviour of the ToString method is to display the number stored in the variable in decimal format as this is the format that is used most of the time.
    To get binary you have to ask for binary:
    VB Code:
    1. Dim fStrm As New FileStream("c:\test.gif", FileMode.Open, FileAccess.Read)
    2.         Dim bytes(9) As Byte
    3.         fStrm.Read(bytes, 0, bytes.Length)
    4.         For i As Integer = 0 To bytes.Length - 1
    5.             RichTextBox2.Text += Convert.ToString(bytes(i), 2).PadLeft(8, "0"c) & vbCrLf
    6.         Next
    7.         fStrm.Close()
    8.         fStrm.Dispose()

    (PadLeft makes sure the string is 8 chars long by adding leading 0's where appropriate - it looks better).

    To get base 16, alter the line to:
    VB Code:
    1. RichTextBox2.Text += Convert.ToString(bytes(i), 16).PadLeft(2, "0"c).ToUpper & vbCrLf
    or
    VB Code:
    1. RichTextBox2.Text += Hex(bytes(i)) & vbCrLf

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Convert Bytes into bits

    Ahh, you were talking about converting to base 2. I was a bit off course to get it. Thanks jo0ls, that solves it.
    VB 2005, Win Xp Pro sp2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width