以下代码可以完成无符号16位数转换成二进制表示的字符串,修改compData还可以转换成12位的二进制,或者
也可以Uint32转换成32位、24位的二进制字符串形式,利用staticResource进行binding,不用编写额外的代码,
就可以数据驱动UI显示。
[valueConversion(typeof(Uint16), typeof(string))]
public class ReverseUintConverter
{
public objectConvert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string strUint = "";
Uint16 compData = 0x8000;
for (int i = 0; i < 16; i++)
{
if (((Uint16)value) & compData) == compData)
{
strUint += "1";
}
else
{
strUint += "0";
}
compData >>= 1;
}
return strUint;
}
public objectConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo.culture)
{
throw new NotImplementedException();
}
}