|
-
Jul 15th, 2006, 11:02 AM
#1
Thread Starter
Frenzied Member
[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:
Dim fStrm As FileStream
Dim bytes(9) As Byte
fStrm = New FileStream("c:\test.gif", FileMode.Open, FileAccess.Read)
fStrm.Read(bytes, 0, bytes.Length)
Dim i As Integer
For i = 0 To bytes.Length - 1
RichTextBox2.Text += bytes(i).ToString & vbCrLf
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?
-
Jul 15th, 2006, 11:58 AM
#2
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:
private string GetBinaryString(Int32 intNumber)
{
Int32 intBinary;
string strReturn = "";
while (intNumber > 0)
{
intBinary = intNumber % 2; //get remainder
strReturn += intBinary;
intNumber = intNumber / 2; //k
}
char[] arrBinary;
arrBinary = strReturn.ToCharArray();
Array.Reverse(arrBinary);
strReturn = new string(arrBinary);
return strReturn;
}
-
Jul 15th, 2006, 12:09 PM
#3
Hyperactive Member
Re: Convert Bytes into bits
or...
VB Code:
Dim b As Byte = 100
Dim binaryString As String = Convert.ToString(b, 2).PadLeft(8, "0"c)
-
Jul 15th, 2006, 12:12 PM
#4
Re: Convert Bytes into bits

D'oh.
-
Jul 16th, 2006, 11:05 AM
#5
Thread Starter
Frenzied Member
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.
-
Jul 16th, 2006, 03:26 PM
#6
Hyperactive Member
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:
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()
(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:
RichTextBox2.Text += Convert.ToString(bytes(i), 16).PadLeft(2, "0"c).ToUpper & vbCrLf
or
VB Code:
RichTextBox2.Text += Hex(bytes(i)) & vbCrLf
-
Jul 17th, 2006, 11:47 AM
#7
Thread Starter
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|