C#中的字符串编码程序

问题描述 投票:0回答:1

测试用例:banagalore(字符串类型) 预期输出:{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);
            }

        }

    }
}
c# string encoding
1个回答
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));
© www.soinside.com 2019 - 2024. All rights reserved.