Writing 32 bit integer to a file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there an easy way of writing a number in 32 bit integer format into four
bytes of a file? I am experimenting with FilePut but I can only make it work
by defining a four byte array and doing some simple calculations to work out
from my integer what the individual values of the four bytes have to be.

Surely there must be a short cut to doing this?

And are there also any existing routines for converting a floating point
number into 32bit IEEE or IBM floating point format and writing the bytes
into a file?

Grateful for any help.
 
Simonc,
Have you looked at the System.IO.BinaryWriter class?

Dim stream As New FileStream("myfile.bin", FileMode.Create)
Dim writer As New System.IO.BinaryWriter(stream)
Dim i As Integer
Dim s As Single
writer.Write(i)
writer.Write(s)

BinaryWriter.Write method is overloaded for most 'primative' types.

Note: BinaryWriter writes strings in a length encoded format, you may want
to use System.Text.Encoding to explicitly convert a string to an array of
bytes first, then write the array of bytes if your string do not follow the
BinaryWriter 'string' format. See BinaryWriter.Write(String) for details.

If you want to convert an Integer to & from a Byte array look at the
System.BitConverter class.

Dim bytes() As Byte = BitConverter.GetBytes(i)

i = BitConverter.ToInt32(bytes, 0)
And are there also any existing routines for converting a floating point
number into 32bit IEEE or IBM floating point format and writing the bytes
into a file?
Floating point numbers (Single) are stored in 32 bit IEEE format. While
Doubles are stored in 64bit IEEE format. Again the BinaryWriter class above
will write singles & doubles. The BitConverter class will convert floating
point numbers to & from a Byte array (I strongly suspect BinaryWriter uses
BitConverter internally).

I should add that System.IO.BinaryReader allows you to read integers &
floats from a file.

Hope this helps
Jay
 
Is there an easy way of writing a number in 32 bit integer format into four
bytes of a file? I am experimenting with FilePut but I can only make it work
by defining a four byte array and doing some simple calculations to work out
from my integer what the individual values of the four bytes have to be.

Surely there must be a short cut to doing this?

And are there also any existing routines for converting a floating point
number into 32bit IEEE or IBM floating point format and writing the bytes
into a file?

Grateful for any help.

System.IO.BinaryWriter...


Here is the example from the .NET framework docs...

Option Explicit On
Option Strict On
Imports System
Imports System.IO
Class MyStream
Private Const FILE_NAME As String = "Test.data"
Public Shared Sub Main()
' Create the new, empty data file.
If File.Exists(FILE_NAME) Then
Console.WriteLine("{0} already exists!", FILE_NAME)
Return
End If
Dim fs As New FileStream(FILE_NAME, FileMode.CreateNew)
' Create the writer for data.
Dim w As New BinaryWriter(fs)
' Write data to Test.data.
Dim i As Integer
For i = 0 To 10
w.Write(CInt(i))
Next i
w.Close()
fs.Close()
' Create the reader for data.
fs = New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
Dim r As New BinaryReader(fs)
' Read data from Test.data.
For i = 0 To 10
Console.WriteLine(r.ReadInt32())
Next i
w.Close()
End Sub
End Class

I would avoid the native VB.NET fileIo functions as much as possible
They are much, much slower then the VB6 equivalents... Using the
classes from System.IO directly gives much more satisfactory performance
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top