Мне пришлось писать свой класс бинарной строки, кстати.
| Код | class BinaryString { private byte[] bs;
public BinaryString() { this.bs = new byte[0]; }
public BinaryString(byte[] byteArray) { this.bs = byteArray; }
public BinaryString(string s) { this.bs = Encoding.UTF8.GetBytes(s); }
public int Length { get { return this.bs.Length; } }
public byte this[int i] { get { return this.bs[i]; } }
public static BinaryString operator +(BinaryString bs1, BinaryString bs2) { byte[] byteArray = new byte[bs1.Length + bs2.Length]; int j = 0;
for (int i = 0; i < bs1.Length; i++, j++) { byteArray[j] = bs1[i]; }
for (int i = 0; i < bs2.Length; i++, j++) { byteArray[j] = bs2[i]; }
return new BinaryString(byteArray); }
public static BinaryString operator +(BinaryString binaryString, byte[] byteArray) { return binaryString + new BinaryString(byteArray); }
public override string ToString() { return new string(Encoding.UTF8.GetChars(this.bs)); }
public byte[] ToByteArray() { return (byte[])this.bs.Clone(); }
public BinaryString Substring(int startIndex, int length) { if (startIndex < 0) { startIndex = bs.Length + startIndex; }
byte[] byteArray = new byte[length];
for (int i = startIndex; i < length; i++) { byteArray[i - startIndex] = this.bs[i]; }
return new BinaryString(byteArray); }
public BinaryString Substring(int startIndex) { return Substring(startIndex, bs.Length); }
public int IndexOf(byte value, int startIndex) { for (int i = startIndex; i < this.Length; i++) { if (this[i] == value) { return i; } }
return -1; }
public int IndexOf(byte value) { return IndexOf(value, 0); }
public int IndexOf(BinaryString value, int startIndex) { bool success;
for (int i = startIndex; i < this.Length - value.Length; i++) { success = true;
for (int j = 0; j < value.Length; j++) { if (this[i + j] != value[j]) { success = false; break; } }
if (success) { return i; } }
return -1; }
public int IndexOf(BinaryString value) { return IndexOf(value, 0); }
public BinaryString Replace(BinaryString oldValue, BinaryString newValue) { BinaryString result = new BinaryString(); BinaryString source = new BinaryString(this.bs);
int p;
while ((p = source.IndexOf(oldValue)) >= 0) { result += source.Substring(0, p) + newValue; source = source.Substring(p + oldValue.Length); }
result += source;
return result; } }
|
|