如何更改WPF中文本框的编码

发布于 2025-01-24 06:54:16 字数 3053 浏览 2 评论 0 原文

我已经写了代码来加密包含UTF8编码的文本。

namespace Yes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static string ourText = File.ReadAllText(@"C:\Users\ASUS\Documents\C#\WPF\Manchester Encoding\bin\Debug\Mancehster.txt", Encoding.UTF8);
        public static byte[] utf_Bytes = Encoding.UTF8.GetBytes(ourText);
        public static byte[] toEncode = new byte[utf_Bytes.Length];
        
        static public void EncodeFunc()
        {
            
            for(int i = 0; i < utf_Bytes.Length; i++)
            {
                string replace = Convert.ToString(utf_Bytes[i], 2);
                string result = Convert.ToString(utf_Bytes[i], 2);
                StringBuilder sb = new StringBuilder(result);
                for(int j = 0; j < replace.Length - 1; j++)
                {

                    if (Convert.ToInt32(replace[j]) == Convert.ToInt32(replace[j + 1]))
                        sb[j] = replace[j];
                                            
                    else                                            
                        sb[j] = replace[j+1];
                   
                }

                if (Convert.ToInt32(replace[replace.Length - 1]) == Convert.ToInt32(replace[0]))
                    sb[replace.Length - 1] = replace[replace.Length - 1];
                else
                    sb[replace.Length - 1] = replace[0];

                result = sb.ToString();
                toEncode[i] = Convert.ToByte(result, 2);
                
            }
        }

        private void Encode_Button_Click(object sender, RoutedEventArgs e)
        {
            EncodeFunc();

            string Encoded_string = Convert.ToString(toEncode[0]);
            StringBuilder sb2 = new StringBuilder(ourText);

            for (int i = 0; i < toEncode.Length; i++)
            {
                string a = Convert.ToString((char)toEncode[i]);
                sb2[i] = a[0];
              
            }

            Encoded_string = sb2.ToString();
            EncodedBox.Text = Encoded_string;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GivenTBox.Text = ourText;
        }
    }
}

在encode_button_click事件中i有 encodedbox.text = encoded_string;

encodedbox textbox

<TextBox x:Name="EncodedBox"   IsReadOnly="True" Margin="0 5 0 5" TextWrapping="Wrap" Height="25" Width="178" VerticalAlignment="Center"/>

问题是问题是 textbox 未在之后显示某些符号代码更改其值。 例如,我的代码将“ H”字母更改为utf8表中的值17的符号,但是 textbox 不显示此符号

net/e2zuu.png“ rel =“ nofollow noreferrer”> 文本框没有显示许多符号。我应该更改如何制作 TextBox 显示它们?

I have written code to encrypt text that has UTF8 encoding.

namespace Yes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static string ourText = File.ReadAllText(@"C:\Users\ASUS\Documents\C#\WPF\Manchester Encoding\bin\Debug\Mancehster.txt", Encoding.UTF8);
        public static byte[] utf_Bytes = Encoding.UTF8.GetBytes(ourText);
        public static byte[] toEncode = new byte[utf_Bytes.Length];
        
        static public void EncodeFunc()
        {
            
            for(int i = 0; i < utf_Bytes.Length; i++)
            {
                string replace = Convert.ToString(utf_Bytes[i], 2);
                string result = Convert.ToString(utf_Bytes[i], 2);
                StringBuilder sb = new StringBuilder(result);
                for(int j = 0; j < replace.Length - 1; j++)
                {

                    if (Convert.ToInt32(replace[j]) == Convert.ToInt32(replace[j + 1]))
                        sb[j] = replace[j];
                                            
                    else                                            
                        sb[j] = replace[j+1];
                   
                }

                if (Convert.ToInt32(replace[replace.Length - 1]) == Convert.ToInt32(replace[0]))
                    sb[replace.Length - 1] = replace[replace.Length - 1];
                else
                    sb[replace.Length - 1] = replace[0];

                result = sb.ToString();
                toEncode[i] = Convert.ToByte(result, 2);
                
            }
        }

        private void Encode_Button_Click(object sender, RoutedEventArgs e)
        {
            EncodeFunc();

            string Encoded_string = Convert.ToString(toEncode[0]);
            StringBuilder sb2 = new StringBuilder(ourText);

            for (int i = 0; i < toEncode.Length; i++)
            {
                string a = Convert.ToString((char)toEncode[i]);
                sb2[i] = a[0];
              
            }

            Encoded_string = sb2.ToString();
            EncodedBox.Text = Encoded_string;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GivenTBox.Text = ourText;
        }
    }
}

In Encode_Button_Click event I have EncodedBox.Text = Encoded_string;

EncodedBox TextBox has following xaml code

<TextBox x:Name="EncodedBox"   IsReadOnly="True" Margin="0 5 0 5" TextWrapping="Wrap" Height="25" Width="178" VerticalAlignment="Center"/>

The problem is that TextBox is not showing some symbols after the code changes its values.
For example, my code changes the "H" letter to a symbol with value 17 in the UTF8 table, but TextBox doesn't show this symbol

Here is what I get

As you can see Changed Text TextBox doesn't show lots of symbols. What should I change to make TextBox show them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文