c# - How to parse text data from a binary file -
hi have binary file contains lots of resources , using c# want find , parse text objects in file in ascii below
lots of binary junk before
onmap 0 131072 "description " 0 "name" "flag" "flag" 7900.000000 0.000000 1499.999268 2.000000 6.000000 8.000000 1.000000 1.000000 1.000000 0 0 0 -1 1 0 0 -1 0.101900 2 36 255 followed line break
lots of binary junk after these objects
each object begins tag onmap here, values separated white space , strings double quoted , must read in order written, don’t know data in file want search through binary until onmap found , read properties list once found onmap dont know how parse properties in.
i recommend locating beginning of string getting format of file contain points beginning of string. code below work under situations, not guaranteed. gnu utilities same. utilities should used quick solutions. binary data pseudo random , possible 5 character match possible, remote.
using system; using system.collections.generic; using system.linq; using system.text; using system.io; namespace consoleapplication1 { class program { enum state { find_onmap, find_return, done } const string filename = @"c:\temp\test.txt"; static void main(string[] args) { list<byte> onmap = encoding.utf8.getbytes("onmap").tolist(); filestream stream = file.openread(filename); int data = 0; state state = state.find_onmap; list<byte> buffer = new list<byte>(); while ((data = stream.readbyte()) != -1) { switch (state) { case state.find_onmap: if (buffer.count < 5) { buffer.add((byte)(data & 0xff)); } else { buffer.removeat(0); buffer.add((byte)(data & 0xff)); } if (buffer.sequenceequal(onmap)) { state = state.find_return; } break; case state.find_return: if (data == 10) { state = state.done; break; } else { buffer.add((byte)(data & 0xff)); } break; } if (state == state.done) break; } if (state == state.done) { string results = encoding.utf8.getstring(buffer.toarray()); console.writeline(results); console.readline(); } } } }
Comments
Post a Comment