|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why does BinaryWriter write garbage before my stuffstrHeader) is just a string, then there is some Binary Data and then again a very short string. I find that at the start of my file, there are some (maybe 1 or 2) higher order ASCII characters (they vary). How can I prevent the program from putting that garbage before my data? Dim fsWriteStream As FileStream = New FileStream(myFileName, FileMode.Create) Dim bwWriter As BinaryWriter = New BinaryWriter(fsWriteStream) bwWriter.Write(strHeader) bwWriter.Write(myBinaryData) bwWriter.Write(strSliceInfo) bwWriter.Close() TIA > (maybe 1 or 2) higher order ASCII characters (they vary). How can I Use a normal streamwriter or any other textwriter.> prevent the program from putting that garbage before my data? > By the way, what do you mean with higer order ASCII characters? I only know 127 ASCII characters which are fitted in 7 bites. Do you mean by instance Extended ASCII characters or something like that. Don't bother about that, as long as they fit in a byte they for sure will fit in unicode. I assume that you are not busy for a compact device with less than 12Kb memory. Cor Anil Gupte wrote:
Show quoteHide quote > I am using the following code to write a file. The initial part (i.e. What's written before the string is the string length. The length is > strHeader) is just a string, then there is some Binary Data and then again a > very short string. I find that at the start of my file, there are some > (maybe 1 or 2) higher order ASCII characters (they vary). How can I prevent > the program from putting that garbage before my data? > > Dim fsWriteStream As FileStream = New FileStream(myFileName, > FileMode.Create) > Dim bwWriter As BinaryWriter = New BinaryWriter(fsWriteStream) > bwWriter.Write(strHeader) > bwWriter.Write(myBinaryData) > bwWriter.Write(strSliceInfo) > bwWriter.Close() > > TIA written using the Write7BitEncodedInt method, so the length may vary. Then the string is encoded (in your case using the default UTF-8 encoding) and the bytes are written to the stream. If you want to use a BinaryReader to read the file, this is the format that you want. If you want to create a file in a specific format, you should write directly to the stream instead. In that case UTF-8 might not be the encoding that you want either. You said: "What's written before the string is the string length. The length
is written using the Write7BitEncodedInt method," But I don't want it to be written - how do I make it stop doing that? You said: "If you want to create a file in a specific format, you should write directly to the stream instead. " I don't understand what that means. Can you please explain? TIA Show quoteHide quote "Göran Andersson" <gu***@guffa.com> wrote in message news:O65083pJIHA.5400@TK2MSFTNGP04.phx.gbl... > Anil Gupte wrote: >> I am using the following code to write a file. The initial part (i.e. >> strHeader) is just a string, then there is some Binary Data and then >> again a very short string. I find that at the start of my file, there >> are some (maybe 1 or 2) higher order ASCII characters (they vary). How >> can I prevent the program from putting that garbage before my data? >> >> Dim fsWriteStream As FileStream = New FileStream(myFileName, >> FileMode.Create) >> Dim bwWriter As BinaryWriter = New BinaryWriter(fsWriteStream) >> bwWriter.Write(strHeader) >> bwWriter.Write(myBinaryData) >> bwWriter.Write(strSliceInfo) >> bwWriter.Close() >> >> TIA > > What's written before the string is the string length. The length is > written using the Write7BitEncodedInt method, so the length may vary. > > Then the string is encoded (in your case using the default UTF-8 encoding) > and the bytes are written to the stream. > > If you want to use a BinaryReader to read the file, this is the format > that you want. If you want to create a file in a specific format, you > should write directly to the stream instead. In that case UTF-8 might not > be the encoding that you want either. > > -- > Göran Andersson > _____ > http://www.guffa.com Anil Gupte wrote:
> You said: "What's written before the string is the string length. The length By not using a BinaryWriter.> is written using the Write7BitEncodedInt method," > > But I don't want it to be written - how do I make it stop doing that? > You said: "If you want to create a file in a specific format, you should Use a FileStream object to write to the file. Use an encoding object to > write directly to the stream instead. " > > I don't understand what that means. Can you please explain? encode the strings into byte arrays so that you can write them to the file. Can you explain what you mean by "Use a normal streamwriter or any other
textwriter"? I need to write a combination of strings (for a header) and binary data (encrypted). A textwriter will not work, ya? By higher order Ascii characters, I mean gobbledygook as in characters beyond Ascii 127. " I assume that you are not busy for a compact device with less than 12Kb memory." Huh? TIA, Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:e3xsy1eJIHA.3356@TK2MSFTNGP02.phx.gbl... >> (maybe 1 or 2) higher order ASCII characters (they vary). How can I >> prevent the program from putting that garbage before my data? >> > Use a normal streamwriter or any other textwriter. > > By the way, what do you mean with higer order ASCII characters? > I only know 127 ASCII characters which are fitted in 7 bites. Do you mean > by instance Extended ASCII characters or something like that. Don't bother > about that, as long as they fit in a byte they for sure will fit in > unicode. > > I assume that you are not busy for a compact device with less than 12Kb > memory. > > Cor >
Shadow Copy Wrapper?
Build a string for arithmetic operation clearing dataset or datatable Force form to foreground? Using an Excel sheet as a DB table Replacing control arrays in .NET Structures and Delegates and ByRef Arguments Split a large application Setup windows service Problem drawing a WMF File VB.NET2005 (and Working in VB6) |
|||||||||||||||||||||||