将十六进制字符串(来自 WPF 文本框)转换为字节
Converting a Hex String (from WPF TextBox) into byte
我编写小型 WPF 应用程序。在我的应用程序中,我有一个名为 Seq
的字段,它实际上是字节数据。
因此 Seq
字段应通过 WPF 应用程序的 TextBox
进行更新。但文本字符串将以十六进制格式输入,不带前导 0x。
基本上我需要写下算法来完成Seq
的set方法只设置一个字节的数据
通过文本框更新的对象 class:
public class WProtocol {
private byte _seq
public byte Seq {
get {
return _seq;
}
set {
_seq = value;
}
}
}
WFrameWindow.xaml.cs
:
public partial class WFrameWindow: Window {
WProtocol m_WProtocol = new WProtocol();
public WFrameWindow() {
InitializeComponent();
this.DataContext = m_WProtocol;
}
}
来自 WFrameWindow.xaml
的代码段显示绑定来源:
<TextBox HorizontalAlignment="Left" Height="24" Margin="115,26,0,0" TextWrapping="Wrap" Text="{***Binding Seq,Mode=OneWayToSource , UpdateSourceTrigger=PropertyChanged***}" VerticalAlignment="Top" Width="99" FontSize="9" FontFamily="Arial"/>
您的解决方案是;
private string _seq_string;
public string SeqString
{
get { return _seq_string; }
set
{
_seq_string = value;
SeqBytes = StringToByteArray(value);
}
}
public byte[] SeqBytes { get; set; }
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
和XAML代码为:
<TextBox HorizontalAlignment="Left" Height="24" Margin="115,26,0,0" TextWrapping="Wrap" Text="{***Binding SeqString,Mode=OneWayToSource , UpdateSourceTrigger=PropertyChanged***}" VerticalAlignment="Top" Width="99" FontSize="9" FontFamily="Arial"/>
我建议的答案是在 ViewModel 中使用转换器而不是代码:
XamlCode:
<TextBox HorizontalAlignment="Left" Height="24" Margin="115,26,0,0" TextWrapping="Wrap"
Text="{Binding Seq, Converter={StaticResource StringToByteConverter}, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Top" Width="99" FontSize="9" FontFamily="Arial"/>
转换器:
public class StringToByteConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is String)
{
string valueTyped = (String)value;
if (String.IsNullOrEmpty(valueTyped) == false && valueTyped.Length <= 2)
return System.Convert.ToByte(valueTyped, 16);
}
return new byte();
}
}
要使用转换器,请将其添加到资源中:
...
xmlns:local="clr-namespace:MyProject"
...
<Application.Resources>
<local:StringToByteConverter x:Key="StringToByteConverter"/>
我编写小型 WPF 应用程序。在我的应用程序中,我有一个名为 Seq
的字段,它实际上是字节数据。
因此 Seq
字段应通过 WPF 应用程序的 TextBox
进行更新。但文本字符串将以十六进制格式输入,不带前导 0x。
基本上我需要写下算法来完成Seq
的set方法只设置一个字节的数据
通过文本框更新的对象 class:
public class WProtocol {
private byte _seq
public byte Seq {
get {
return _seq;
}
set {
_seq = value;
}
}
}
WFrameWindow.xaml.cs
:
public partial class WFrameWindow: Window {
WProtocol m_WProtocol = new WProtocol();
public WFrameWindow() {
InitializeComponent();
this.DataContext = m_WProtocol;
}
}
来自 WFrameWindow.xaml
的代码段显示绑定来源:
<TextBox HorizontalAlignment="Left" Height="24" Margin="115,26,0,0" TextWrapping="Wrap" Text="{***Binding Seq,Mode=OneWayToSource , UpdateSourceTrigger=PropertyChanged***}" VerticalAlignment="Top" Width="99" FontSize="9" FontFamily="Arial"/>
您的解决方案是;
private string _seq_string;
public string SeqString
{
get { return _seq_string; }
set
{
_seq_string = value;
SeqBytes = StringToByteArray(value);
}
}
public byte[] SeqBytes { get; set; }
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
和XAML代码为:
<TextBox HorizontalAlignment="Left" Height="24" Margin="115,26,0,0" TextWrapping="Wrap" Text="{***Binding SeqString,Mode=OneWayToSource , UpdateSourceTrigger=PropertyChanged***}" VerticalAlignment="Top" Width="99" FontSize="9" FontFamily="Arial"/>
我建议的答案是在 ViewModel 中使用转换器而不是代码:
XamlCode:
<TextBox HorizontalAlignment="Left" Height="24" Margin="115,26,0,0" TextWrapping="Wrap"
Text="{Binding Seq, Converter={StaticResource StringToByteConverter}, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Top" Width="99" FontSize="9" FontFamily="Arial"/>
转换器:
public class StringToByteConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is String)
{
string valueTyped = (String)value;
if (String.IsNullOrEmpty(valueTyped) == false && valueTyped.Length <= 2)
return System.Convert.ToByte(valueTyped, 16);
}
return new byte();
}
}
要使用转换器,请将其添加到资源中:
...
xmlns:local="clr-namespace:MyProject"
...
<Application.Resources>
<local:StringToByteConverter x:Key="StringToByteConverter"/>