简体   繁体   中英

Converting a Hex String (from WPF TextBox) into byte

I write small WPF application. In my app I have one field called Seq which is actually the byte data.

So Seq field should get updated via TextBox of the WPF application. But Text string will be typed in Hex format without leading 0x.

Basically I need to write down the algorithm to complete the set method of Seq to set only one byte of data.

Object class which gets updated via text box:

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;
    }
}

Snippet from WFrameWindow.xaml to show binding the of the source:

<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"/>

Your solution would be;

    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();
    }

and XAML code would be:

<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"/>

My proposed answer is using a converter instead of code in the ViewModel:

The 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"/>

The Converter:

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();

    }
}

To use the converter, add it in the Resources:

...
xmlns:local="clr-namespace:MyProject"
...

<Application.Resources>
    <local:StringToByteConverter x:Key="StringToByteConverter"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM