情報アイランド

「情報を制する者は世界を制す」をモットーに様々な情報を提供することを目指すブログです。現在はプログラミング関連情報が多めですが、投資関連情報も取り扱っていきたいです。

C#でデータをバイナリに変換する

シリアライズすれば良いのかもしれませんが・・・。

一つ一つ変換するのが面倒だったので、作ってみました。

よく使いそうな型(+独自の型)にしか対応していません。また、リストのリストなどはCREAIBinaryEncodableなどでラップしてやるしかなさそうです。

public static class CREABinaryEncoderHelper
{
    static CREABinaryEncoderHelper()
    {
    }

    public static void Write(bool value, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(value), 0, 1);
    }

    public static void Write(double value, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(value), 0, 8);
    }

    public static void Write(int value, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(value), 0, 4);
    }

    public static void Write(DateTime value, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(value.ToBinary()), 0, 8);
    }

    public static void Write(TimeSpan value, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(value.Ticks), 0, 8);
    }

    public static void Write(string value, MemoryStream memoryStream)
    {
        byte[] data = Encoding.UTF8.GetBytes(value);
        memoryStream.Write(BitConverter.GetBytes(data.Length), 0, 4);
        memoryStream.Write(data, 0, data.Length);
    }

    public static void Write(byte[] value, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(value.Length), 0, 4);
        memoryStream.Write(value, 0, value.Length);
    }

    public static void Write(CREAIBinaryEncodable value, MemoryStream memoryStream)
    {
        byte[] data = value.ToBinary();
        memoryStream.Write(BitConverter.GetBytes(data.Length), 0, 4);
        memoryStream.Write(data, 0, data.Length);
    }

    public static void Write<T>(List<T> values, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(values.Count), 0, 4);
        foreach (var value in values)
        {
            byte[] binary = CREABinaryEncoder.Encode(value);
            memoryStream.Write(binary, 0, binary.Length);
        }
    }

    public static void Write<T>(ObservableCollection<T> values, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(values.Count), 0, 4);
        foreach (var value in values)
        {
            byte[] binary = CREABinaryEncoder.Encode(value);
            memoryStream.Write(binary, 0, binary.Length);
        }
    }

    public static void Write<T, U>(Dictionary<T, U> values, MemoryStream memoryStream)
    {
        memoryStream.Write(BitConverter.GetBytes(values.Count), 0, 4);
        foreach (var value in values)
        {
            byte[] binaryKey = CREABinaryEncoder.Encode(value.Key);
            memoryStream.Write(binaryKey, 0, binaryKey.Length);
            byte[] binaryValue = CREABinaryEncoder.Encode(value.Value);
            memoryStream.Write(binaryValue, 0, binaryValue.Length);
        }
    }
}

public static class CREABinaryEncoder
{
    public static byte[] Encode(params object[] parameters)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            foreach (var parameter in parameters)
                if (parameter is bool)
                    CREABinaryEncoderHelper.Write((bool)parameter, memoryStream);
                else if (parameter is double)
                    CREABinaryEncoderHelper.Write((double)parameter, memoryStream);
                else if (parameter is int)
                    CREABinaryEncoderHelper.Write((int)parameter, memoryStream);
                else if (parameter is DateTime)
                    CREABinaryEncoderHelper.Write((DateTime)parameter, memoryStream);
                else if (parameter is TimeSpan)
                    CREABinaryEncoderHelper.Write((TimeSpan)parameter, memoryStream);
                else if (parameter is string)
                    CREABinaryEncoderHelper.Write((string)parameter, memoryStream);
                else if (parameter is byte[])
                    CREABinaryEncoderHelper.Write((byte[])parameter, memoryStream);
                else if (parameter is CREAIBinaryEncodable)
                    CREABinaryEncoderHelper.Write((CREAIBinaryEncodable)parameter, memoryStream);
                else if (parameter is List<bool>)
                    CREABinaryEncoderHelper.Write((List<bool>)parameter, memoryStream);
                else if (parameter is List<double>)
                    CREABinaryEncoderHelper.Write((List<double>)parameter, memoryStream);
                else if (parameter is List<int>)
                    CREABinaryEncoderHelper.Write((List<int>)parameter, memoryStream);
                else if (parameter is List<DateTime>)
                    CREABinaryEncoderHelper.Write((List<DateTime>)parameter, memoryStream);
                else if (parameter is List<TimeSpan>)
                    CREABinaryEncoderHelper.Write((List<TimeSpan>)parameter, memoryStream);
                else if (parameter is List<string>)
                    CREABinaryEncoderHelper.Write((List<string>)parameter, memoryStream);
                else if (parameter is List<CREAIBinaryEncodable>)
                    CREABinaryEncoderHelper.Write((List<CREAIBinaryEncodable>)parameter, memoryStream);
                else if (parameter is Dictionary<int, bool>)
                    CREABinaryEncoderHelper.Write((Dictionary<int, bool>)parameter, memoryStream);
                else if (parameter is Dictionary<int, double>)
                    CREABinaryEncoderHelper.Write((Dictionary<int, double>)parameter, memoryStream);
                else if (parameter is Dictionary<int, int>)
                    CREABinaryEncoderHelper.Write((Dictionary<int, int>)parameter, memoryStream);
                else if (parameter is Dictionary<int, string>)
                    CREABinaryEncoderHelper.Write((Dictionary<int, string>)parameter, memoryStream);
                else if (parameter is Dictionary<int, CREAIBinaryEncodable>)
                    CREABinaryEncoderHelper.Write((Dictionary<int, CREAIBinaryEncodable>)parameter, memoryStream);
                else if (parameter is Dictionary<string, bool>)
                    CREABinaryEncoderHelper.Write((Dictionary<string, bool>)parameter, memoryStream);
                else if (parameter is Dictionary<string, double>)
                    CREABinaryEncoderHelper.Write((Dictionary<string, double>)parameter, memoryStream);
                else if (parameter is Dictionary<string, int>)
                    CREABinaryEncoderHelper.Write((Dictionary<string, int>)parameter, memoryStream);
                else if (parameter is Dictionary<string, string>)
                    CREABinaryEncoderHelper.Write((Dictionary<string, string>)parameter, memoryStream);
                else if (parameter is Dictionary<string, CREAIBinaryEncodable>)
                    CREABinaryEncoderHelper.Write((Dictionary<string, CREAIBinaryEncodable>)parameter, memoryStream);
                else if (parameter is Dictionary<CREAIBinaryEncodable, bool>)
                    CREABinaryEncoderHelper.Write((Dictionary<CREAIBinaryEncodable, bool>)parameter, memoryStream);
                else if (parameter is Dictionary<CREAIBinaryEncodable, double>)
                    CREABinaryEncoderHelper.Write((Dictionary<CREAIBinaryEncodable, double>)parameter, memoryStream);
                else if (parameter is Dictionary<CREAIBinaryEncodable, int>)
                    CREABinaryEncoderHelper.Write((Dictionary<CREAIBinaryEncodable, int>)parameter, memoryStream);
                else if (parameter is Dictionary<CREAIBinaryEncodable, string>)
                    CREABinaryEncoderHelper.Write((Dictionary<CREAIBinaryEncodable, string>)parameter, memoryStream);
                else if (parameter is Dictionary<CREAIBinaryEncodable, CREAIBinaryEncodable>)
                    CREABinaryEncoderHelper.Write((Dictionary<CREAIBinaryEncodable, CREAIBinaryEncodable>)parameter, memoryStream);
                else if (parameter is ObservableCollection<CREAIBinaryEncodable>)
                    CREABinaryEncoderHelper.Write((ObservableCollection<CREAIBinaryEncodable>)parameter, memoryStream);
                else
                    throw new NotSupportedException("対応していない型です。");

            return memoryStream.ToArray();
        }
    }
}
pizyumi
プログラミング歴19年のベテランプログラマー。業務システム全般何でも作れます。現在はWeb系の技術を勉強中。
スポンサーリンク

-C#