将文本转换为带坐标的数字

时间:2014-03-20 19:44:22

标签: c# winforms encryption coordinate-systems coordinate

我想今天为了好玩而做些事情,并决定制作一个简单的程序,将文本的和平翻译成数字。我试过这段代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlphaNumCrypt
{
public partial class Form1 : Form
{
    public String input;
    public String output;
    public Form1()
    {
        InitializeComponent();
    }

    private void translateButton_Click(object sender, EventArgs e)
    {
        // Encrypt/Decrypt input
        if (toText.Checked == true)
        {
            // Decrypt input
            input = "";
            input = inputTextBox.Text.ToString();
            decrypt();
        }
        else if (toAlphaNum.Checked == true)
        {
            // Encrypt input
            input = "";
            input = inputTextBox.Text.ToString();
            encrypt();
        }
    }

    private void encrypt()
    {
        // Encrypt the plain text input
        output = input;
        output.Replace("a", "11");
        output.Replace("b", "12");
        output.Replace("c", "13");
        output.Replace("d", "14");
        output.Replace("e", "15");
        output.Replace("f", "16");
        output.Replace("g", "17");
        output.Replace("h", "18");
        output.Replace("i", "19");
        output.Replace("j", "21");
        output.Replace("k", "22");
        output.Replace("l", "23");
        output.Replace("m", "24");
        output.Replace("n", "25");
        output.Replace("o", "26");
        output.Replace("p", "27");
        output.Replace("q", "28");
        output.Replace("r", "29");
        output.Replace("s", "31");
        output.Replace("t", "32");
        output.Replace("u", "33");
        output.Replace("v", "34");
        output.Replace("w", "35");
        output.Replace("x", "36");
        output.Replace("y", "37");
        output.Replace("z", "38");
        output.Replace("æ", "39");
        output.Replace("ø", "41");
        output.Replace("å", "42");
        outputTextBox.Text = output;
    }

    private void decrypt()
    {
        // Decrypt the AlphaNum input
        output = input;
        output.Replace("11", "a");
        output.Replace("12", "b");
        output.Replace("13", "c");
        output.Replace("14", "d");
        output.Replace("15", "e");
        output.Replace("16", "f");
        output.Replace("17", "g");
        output.Replace("18", "h");
        output.Replace("19", "i");
        output.Replace("21", "j");
        output.Replace("22", "k");
        output.Replace("23", "l");
        output.Replace("24", "m");
        output.Replace("25", "n");
        output.Replace("26", "o");
        output.Replace("27", "p");
        output.Replace("28", "q");
        output.Replace("29", "r");
        output.Replace("31", "s");
        output.Replace("32", "t");
        output.Replace("33", "u");
        output.Replace("34", "v");
        output.Replace("35", "w");
        output.Replace("36", "x");
        output.Replace("37", "y");
        output.Replace("38", "z");
        output.Replace("39", "æ");
        output.Replace("41", "ø");
        output.Replace("42", "å");
        outputTextBox.Text = output;
    }
}
}

但由于某种原因,输出等于输入,我在这里做错了什么?

此致 埃里克

4 个答案:

答案 0 :(得分:1)

这是因为您没有使用output.Replace方法的结果。它返回一个新字符串,并且不修改现有字符串。您应该按如下方式修改代码:

output = output.Replace("a", "11");
...

<强>更新即可。为了使代码更方便,您可以将转换规则存储为Tuple<string, string>数组。因此,您可以使用循环来编码和解码文本。这是一个例子:

private readonly Tuple<string, string>[] rules = new[]
    {
        new Tuple<string, string>("a", "11"),
        new Tuple<string, string>("b", "12"),
        new Tuple<string, string>("c", "13"),
        ...
    };

private string Encrypt(string input)
{
    return rules.Aggregate(input, (current, rule) => current.Replace(rule.Item1, rule.Item2));
}

private string Decrypt(string input)
{
    return rules.Aggregate(input, (current, rule) => current.Replace(rule.Item2, rule.Item1));
}

答案 1 :(得分:1)

您必须存储output.Replace返回的结果,例如:

output = input;
output = output.Replace("11", "a");
output = output.Replace("12", "b");
        ...
outputTextBox.Text = output;

答案 2 :(得分:1)

.Replace()方法是非破坏性的。它不会修改原始对象。它仅在应用了函数的情况下返回基于原始值的新值。所以你要做的就是将对象重新分配给返回的内容:

output = output.Replace("o", "26");

你也可以考虑创建一些字典来处理映射,只是循环遍历每个字母而不是调用每个字母,无论它是否出现在原始字符串中。

答案 3 :(得分:0)

为了完整性:

在您的代码中使用StringBuilder代替string进行输出和输入。 然后你不必重新分配所有替换线。您的代码应该像-is一样工作。只需从output = inputinput = output函数

中删除encrypt()decrypt()

来自MSDN:

Although StringBuilder and String both represent sequences of characters, they are implemented differently. String is an immutable type. That is, each operation that appears to modify a String object actually creates a new string.

For routines that perform extensive string manipulation (such as apps that modify a string numerous times in a loop), modifying a string repeatedly can exact a significant performance penalty. The alternative is to use StringBuilder, which is a mutable string class. Mutability means that once an instance of the class has been created, it can be modified by appending, removing, replacing, or inserting characters.