简体   繁体   中英

C# Converting a String to Bytes

I have a program that uses encryption text strings, I basically just want to write a simple program that will print out all of the decrypted text for me.

For example: Say that the letter "A" = the byte "2C"; I would want to type the letter A in to the program and have it print out "2C" for me.

Does anybody know an easy way to do this?

Many thanks!

By 2C I think you mean the hex representation of the letter A ?

That would be something like String.Format("{0:X}", Convert.ToInt32('A'));

Update after clarification from OP

You either need to predefine your entire supported character set like this.

    static Dictionary<char, int> cyper = new Dictionary<char, int>
{
{'A', 44},
{'B', 45},
{'C', 46},
{'D', 47},
{'E', 48},
{'F', 49},
// .. etc
};

// ...

        Console.WriteLine(string.Format("{0:X}", cyper['A'])); // will print 2C

But that doesn't seem like a very good encryption if everything is just off by a few values.

Another approach would be to apply an encoding scheme. A runtime mathematical evaluation on the input that will evaluate to 2C (encrypt) and be able to take 2C and evaluate to A (decrypt).

我建议您尝试这样:

byte[] b = System.Text.Encoding.UTF8.GetBytes (yourString);

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