Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read mixed file of byte and string
    text
    copied!<p>I've a mixed file with a lot of string line and part of byte encoded data. Example: </p> <pre><code>--Begin Attach Content-Info: /Format=TIF Content-Description: 30085949.tif (TIF File) Content-Transfer-Encoding: binary; Length=220096 II*II* Îh ÿÿÿÿÿÿü³küìpsMg›Êq™Æ™Ôd™‡–h7ÃAøAú áùõ=6?Eã½/ô|û ƒú7z:&gt;„Çÿý&lt;þ¯úýúßj?å¿þÇéöûþ“«ÿ¾ÁøKøÈ%ŠdOÿÞÈ&lt;,Wþ‡ÿ·ƒïüúCÿß%Ï$sŸÿÃÿ÷‡þåiò&gt;GÈù#ä|‘ò:#ä|Š":#¢:;ˆèŽˆèʤV‘ÑÑÑÑÑÑÑÑÑçIþ×o(¿zHDDDDDFp'.Ñ:ˆR:aAràÁ¬LˆÈù!ÿÿï[ÿ¯Äàiƒ"VƒDÇ)Ê6PáÈê$9C”9C†‡CD¡pE@¦œÖ{i~Úý¯kköDœ4ÉU”8`ƒt!l2G --End Attach-- </code></pre> <p>i try to read file with streamreader:</p> <pre><code>string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Davide\Desktop\20041230000D.xmm") </code></pre> <p>I read line by line the file, and when line is equal "Content-Transfer-Encoding: binary; Length=220096", i read all following lines and write a "filename"(in this case 30085949.tif) file. But i'm reading strings, not byte data and result file is damaged (now i try with tiff file). Any suggestion for me?</p> <p><strong>SOLUTION</strong> Thanks for reply. I've adopted this solution: I builded a LineReader extend BinaryReader:</p> <pre><code> public class LineReader : BinaryReader { public LineReader(Stream stream, Encoding encoding) : base(stream, encoding) { } public int currentPos; private StringBuilder stringBuffer; public string ReadLine() { currentPos = 0; char[] buf = new char[1]; stringBuffer = new StringBuilder(); bool lineEndFound = false; while (base.Read(buf, 0, 1) &gt; 0) { currentPos++; if (buf[0] == Microsoft.VisualBasic.Strings.ChrW(10)) { lineEndFound = true; } else { stringBuffer.Append(buf[0]); } if (lineEndFound) { return stringBuffer.ToString(); } } return stringBuffer.ToString(); } } </code></pre> <p>Where <em>Microsoft.VisualBasic.Strings.ChrW(10)</em> is a Line Feed. When i parse my file:</p> <pre><code> using (LineReader b = new LineReader(File.OpenRead(path), Encoding.Default)) { int pos = 0; int length = (int)b.BaseStream.Length; while (pos &lt; length) { string line = b.ReadLine(); pos += (b.currentPos); if (!beginNextPart) { if (line.StartsWith(BEGINATTACH)) { beginNextPart = true; } } else { if (line.StartsWith(ENDATTACH)) { beginNextPart = false; } else { if (line.StartsWith("Content-Transfer-Encoding: binary; Length=")) { attachLength = Convert.ToInt32(line.Replace("Content-Transfer-Encoding: binary; Length=", "")); byte[] attachData = b.ReadBytes(attachLength); pos += (attachLength); ByteArrayToFile(@"C:\users\davide\desktop\files.tif", attachData); } } } } } </code></pre> <p>I read a byte length from file and i read following n bytes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload