using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Flyad7_WPF.Converters
{
    public class IO2BinConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            UInt16 io = (UInt16)value;
            string str = "";
            for (int i = 0; i < 4; i++) {
                byte b = (byte)((io >> (i * 4)) & 0x0f);
                string str_b = System.Convert.ToString(b, 2);
                if (string.IsNullOrEmpty(str))
                {
                    str = str_b.PadLeft(4, '0');
                }
                else {
                    str = str_b.PadLeft(4, '0') + " " + str;
                }
            }
            return str;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return System.Convert.ToUInt16((string)value, 2);
        }

        #endregion
    }
}