'How to change encoding of TextBox in WPF
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
As you can see Changed Text TextBox doesn't show lots of symbols. What should I change to make TextBox show them?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

