3

I need to create a file that include binary input but when i do this it take as byte not bit

for example if i want to add binary represent of "apple" it writes to file 0110000101110000011100000110110001100101 it contains 40 bits however when i look at the file it show 40bytes because it takes every bit as char and so it save as byte-wise. How can i prevent this and save all information bit-wise in VB.net ?

Dim fs As New FileStream("\binfile.bin", FileMode.Append)
    Dim bw As New BinaryWriter(fs)
    Dim TempStr As String

    For t As Integer = 0 To Nameoftable.Length - 1
        Dim bin As String = _
            LongToBinary(Asc(Nameoftable.Substring(t, 1))) 'BIT CONVERTER FUNCTION
        TempStr &= bin.Substring(bin.Length - 8)
    Next t

        bw.Write(TempStr)

    bw.Close()

Thanks a lot...

3
  • Well, don't use LongToBinary(). It isn't clear why you do. If you want bytes then StreamWriter will work just fine. Commented Apr 7, 2013 at 20:46
  • I use LongToBinary function because, I need to save data as binary because in some part of record I use hashing and bit-wise part needed.. for example i need first bit status of data and after rest of data in this kind of situation i need bitwise record.. The real issue evenif i change all string, integers into binary format, it written as char into .bin file.. how can i prevent this ? Commented Apr 7, 2013 at 21:10
  • Moreover i just try streamwriter, this is almost same as binarywriter for mycase.. I need to write bit-wise.. for example if i add 5 bits into bin file, it must grow 5 bits not 5 bytes.. Commented Apr 7, 2013 at 21:12

1 Answer 1

1

You must use BINARY reader/writer object and also specify the field-type of the data being sent to the write-stream and the same goes for data being read in the reader from the stream.

Dim filename As String = "c:\temp\binfile.bin"
Dim writer As BinaryWriter
Dim reader As BinaryReader
Dim tmpStringData As String
Dim tmpByteData As Byte
Dim tmpCharData As Char
Dim tempIntData as Integer
Dim tempBoolData as Boolean
'
writer = New BinaryWriter(File.Open(filename, FileMode.Append))
Using writer
  writer.Write("apple")
  'writer.Write(YourByteDataHere)   'byte
  'writer.Write(YourCharHere)   'char
  'writer.Write(1.31459)        'single
  'writer.Write(100)        'integer
  'writer.Write(False)        'boolean
End Using
writer.Close()
'
If (File.Exists(filename)) Then
  reader = New BinaryReader(File.Open(filename, FileMode.Open))
  Using reader
    tmpStringData = reader.ReadString()
    'tempByteData = reader.ReadByte()
    'tempCharData = reader.ReadChar()
    'tempSingleData = reader.ReadSingle()
    'tempIntData = reader.ReadInt32()
    'tempBoolData = reader.ReadBoolean()
  End Using
  reader.Close()
End If

I used ReadString() method to write the string "apple" You could use the chr code for a character or byte if you wish, in which case you must use ReadByte() or ReadChar() or ReadInt() depending on how you sent it to the stream (as a byte, char or integer)

So the file size is 6 bytes 1 for its own internal use by the file stream handler and 5 for your 'apple'

And if you saved it as char or byte I would think its 5 bytes used and 1k long And if you saved it as integer I would think its 10 bytes used and 1k long

reference: http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.