日期:2014-05-18 浏览次数:20949 次
var lines = File.ReadAllLines(@"E:\我的项目文档\c.txt"); var sw = new StreamWriter(@"E:\我的项目文档\De.txt", false); var blen = 64; foreach (var item in lines) { string[] blks; var count = item.Length / blen; var rem = item.Length % blen; if (rem > 0) { blks = new string[count + 1]; blks[count] = item.Substring(blen * count, rem); } else { blks = new string[count]; } for (int i = 0; i < count; i++) { blks[i] = item.Substring(blen * i, blen); } for (int i = 0; i < blks.Length; i++) { var str = blks[i]; var idxs = new List<int>(blen); var index = -1; while ((index = item.IndexOf('1', index + 1)) >= 0) { idxs.Add(index); } var istr = i.ToString(); var sidxstr = idxs[0].ToString(); sw.WriteLine(string.Concat("M", istr, "[", sidxstr, "] O[", istr, "] X[", sidxstr, "] P", istr, "[", sidxstr, "]")); var lidx = idxs.Count - 1; for (int j = 1; j < lidx; j++) { var idxstr = idxs[j].ToString(); sw.WriteLine(string.Concat("M", istr, "[", idxstr, "] P", istr, "[", sidxstr, "] X[", idxstr, "] P", istr, "[", idxstr, "]")); sidxstr = idxstr; } var eidxstr = idxs[lidx].ToString(); sw.WriteLine(string.Concat("M", istr, "[", eidxstr, "] P", istr, "[", sidxstr, "] X[", eidxstr, "] I[", istr, "]")); sw.Flush(); } }