private static void Func_EnergyByteArrayBuilder_2(int max) { Random random = new Random(); Energy.Base.ByteArrayBuilder b = new Energy.Base.ByteArrayBuilder(); for (int i = 0; i < max; i++) { b.Append((byte)(random.Next(256))); } byte crc = 0; byte[] d = b.ToArray(); for (int i = 0; i < max; i++) { crc = (byte)((d[i] + crc) % 256); } Console.WriteLine("CRC of " + max + " elements = " + crc); }
public void ByteArrayBuilderAppend() { byte[] source; byte[] expect; source = new byte[] { 1, 2, 3 }; Energy.Base.ByteArrayBuilder bb = new Energy.Base.ByteArrayBuilder(); bb.Append(source); Assert.AreEqual(3, bb.Length); Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(source, bb.ToArray())); bb.Clear(); Assert.AreEqual(0, bb.Length); bb.Append(source, 1, 1); expect = new byte[] { 2 }; Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); bb.Append(source, 2, 2); expect = new byte[] { 2, 3 }; Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); bb.Append(source, 0, 2); expect = new byte[] { 2, 3, 1, 2 }; Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); bb.Append(source, 3, 3); Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); bb.Append(source, 3, 0); Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); bb.Append(source, 2, 3); expect = new byte[] { 2, 3, 1, 2, 3 }; Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); bb.Clear(); bb.Append(source, 2); expect = new byte[] { 1, 2 }; Assert.AreEqual(0, Energy.Base.ByteArrayBuilder.Compare(expect, bb.ToArray())); Assert.AreEqual(2, bb.Length); }
private static void Example2() { Energy.Base.ByteArrayBuilder b = new Energy.Base.ByteArrayBuilder(); b.Append((byte)1); b.Append((byte)2); b.Append((int)1); b.Append((int)2); b.Append("Zurück"); b.Encoding = System.Text.Encoding.Unicode; b.Append("Zurück"); b.Append(Guid.NewGuid()); b.Rewind(); Console.WriteLine(Energy.Base.Hex.Print(b.ToArray())); b.Rewind(); b.Skip(2 + 8); string s1 = b.ReadString(System.Text.Encoding.UTF8); string s2 = b.ReadString(System.Text.Encoding.Unicode); Console.WriteLine("{0} {1}", s1, s2); Guid guid = b.ReadGuid(); Console.WriteLine(Energy.Base.Hex.Print(guid.ToByteArray())); }