/// <summary> /// Reads a string with special circumstance capability /// </summary> /// <param name="xStringSize"></param> /// <param name="xStringType"></param> /// <param name="xRead"></param> /// <returns></returns> public string ReadString(StringForm xStringType, int xStringSize, StringRead xRead) { try { return ReadString(xStringType, xStringSize, xRead, IsBigEndian); } catch (Exception x) { throw x; } }
/// <summary> /// Reads a string with special circumstance capability /// </summary> /// <param name="xStringSize"></param> /// <param name="xStringType"></param> /// <param name="xRead"></param> /// <param name="BigEndian"></param> /// <returns></returns> public string ReadString(StringForm xStringType, int xStringSize, StringRead xRead, bool BigEndian) { if (!Enum.IsDefined(typeof(StringForm), xStringType) || !Enum.IsDefined(typeof(StringRead), xRead)) throw new Exception("Invalid parameters"); try { if (xRead == StringRead.ToNull) { if (Position >= Length - 1) return ""; List<byte> buffer = new List<byte>(); if (xStringType == StringForm.Unicode) { byte[] buff = ReadBytes(2); int i = 0; while (xStringSize == 0 || i < xStringSize) { if (buff[0] == 0 && buff[1] == 0) break; try { buffer.AddRange(buff); buff = ReadBytes(2); } catch { break; } } if (BigEndian) return Encoding.BigEndianUnicode.GetString(buffer.ToArray()).Replace(PadType.Null.ToString(), ""); else return Encoding.Unicode.GetString(buffer.ToArray()).Replace(PadType.Null.ToString(), ""); } else { if (Position >= Length) return ""; byte buff = ReadByte(); int i = 0; while (xStringSize == 0 || i < xStringSize) { if (buff == 0) break; try { buffer.Add(buff); buff = ReadByte(); } catch { break; } i++; } return Encoding.ASCII.GetString(buffer.ToArray()).Replace(PadType.Null.ToString(), ""); } } else if (xRead == StringRead.PrecedingLength) { List<byte> Buffer = new List<byte>(); byte len = ReadByte(); for (int i = 0; i < len; i++) { if (xStringType == StringForm.ASCII) Buffer.AddRange(ReadBytes(2)); else Buffer.Add(ReadByte()); } if (xStringType == StringForm.ASCII) return Encoding.ASCII.GetString(Buffer.ToArray()).Replace(PadType.Null.ToString(), ""); else { if (BigEndian) return Encoding.BigEndianUnicode.GetString(Buffer.ToArray()).Replace(PadType.Null.ToString(), ""); else return Encoding.Unicode.GetString(Buffer.ToArray()).Replace(PadType.Null.ToString(), ""); } } else { byte[] buff = ReadBytes((byte)xStringType * xStringSize); if (xStringType == StringForm.ASCII) return Encoding.ASCII.GetString(buff).Replace(PadType.Null.ToString(), ""); else { if (BigEndian) return Encoding.BigEndianUnicode.GetString(buff).Replace(PadType.Null.ToString(), ""); else return Encoding.Unicode.GetString(buff).Replace(PadType.Null.ToString(), ""); } } } catch (Exception x) { throw x; } }
/// <summary> /// Reads a string with special circumstance capability /// </summary> /// <param name="xStringSize"></param> /// <param name="xStringType"></param> /// <param name="xRead"></param> /// <returns></returns> public string ReadString(StringForm xStringType, int xStringSize, StringRead xRead) { { return ReadString(xStringType, xStringSize, xRead, IsBigEndian); } //throw x }