Pulling bytes from a file

tw607

New member
Joined
Aug 15, 2007
Messages
2
Programming Experience
Beginner
Hi everyone,
I have a seemingly simple problem which has been driving me crazy. I have a file consisting of a number of bytes and I want to take out eight bytes at a time, combine all eight bytes into a string, convert the new string to decimal and output the decimal to a text file. Here is my code that I've been working with:



VB.NET:
                  Dim index As Integer = 8
                  For i = 0 To s1.Length() - 1 
                      bytes(i) = s1.ReadByte
                      If (i < index) Then
                          s2.WriteByte(bytes(i))
                      End If
                      index = index + 8
                  Next

Here s1 is my filestream reader and s2 is my filestream writer. So far this prints out to the writer file the same thing as the reader file. I am not familiar with working with bytes and this has been driving me crazy. Can someone please help me out. Thanks!
 
  1. Declare an 8 bytes byte array as buffer, use the FileStream.Read to fill this, Read returns 0 when it doesn't read more (end of file).
  2. To convert each byte buffer to a string you have to use the text encoding originally used to write it, for example System.Text.Encoding.UTF8, this has the GetString method that takes the buffer array as parameter.
  3. Decimal.Parse can be used to convert String to Decimal, for example to a currentDecimal variable.
  4. For output you can use the IO.StreamWriter and the WriteLine method to write currentDecimal.ToString.
 
Back
Top