Tuesday

BinBuilder - equivalent of StringBuilder for binary objects

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public class BinBuilder
{

    public BinaryWriter writer;
    public MemoryStream requestMemoryStream = new MemoryStream();

    public BinBuilder(string a)
        : this(ASCIIEncoding.Default.GetBytes(a))
    {
    }

    public  BinBuilder(byte[] init):this()
    {
        writer.Write(init);
    }

    public void Append(ushort us)
    {
        this.Append(BitConverter.GetBytes(us));
    }

    public void AppendCRC(ushort us)
    {
        var a=BitConverter.GetBytes(us);
        writer.Write(a[1]);
        writer.Write(a[0]);
    }

    public void Append(char us)
    {
        this.Append(BitConverter.GetBytes((ushort)us));
    }


    public void Append(byte[] init)
    {
        writer.Write(init);
    }


    public BinBuilder()
    {
        writer = new BinaryWriter(requestMemoryStream);
    }

    public byte[] ToArray()
    {
        writer.Flush();
        return requestMemoryStream.ToArray();
    }

}

No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...