字符串编码程序

时间:2014-10-20 14:42:55

标签: c# string encoding

测试用例:

banagalore (of type String)

预期产出:

{30,20,21,92,20,80,32,31,02}

我已经使用C#将它们转换为ASCII,现在我无法将它们转换为该序列,请提出一些想法。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Enter The String ");
            string str = Console.ReadLine();
            byte[] b = Encoding.ASCII.GetBytes(str);
            foreach (var item in b)
            {
                Console.WriteLine(item);
            }

        }

    }
}

1 个答案:

答案 0 :(得分:0)

XOR掩码是一种选择。

byte[] input = Encoding.ASCII.GetBytes("bangalore");
var known_result = new byte[] { 30, 20, 21, 92, 20, 80, 32, 31, 02 };
var computed_mask = new byte[input.Length];

for (var i = 0; i < input.Length; i++)
{
    computed_mask[i] = (byte)(known_result[i] ^ input[i]);
}

byte[] test = Encoding.ASCII.GetBytes("bangalore");
for (var i = 0; i < test.Length; i++)
{
    test[i] ^= computed_mask[i];
}
Console.WriteLine(string.Join(",", test));