在矩阵中读取/插入数组元素

时间:2016-10-23 04:17:39

标签: c# arrays string matrix

我有一个带有二进制值的数组,我将它展开并将它们插入到数组中,但现在我想将这些元素移动到[8,8]矩阵(每行一行),我该怎么做呢?

我一直在尝试,但我找不到任何好的例子

static void Main(string[] args)
{
    string textonorm, textobin = "", textobin1 = "", textocod = "";
    int textval, quant, result, txtvalor;

    Console.WriteLine("Digite a frase que deseja criptografar!");
    textonorm = Console.ReadLine();

    textval = System.Text.ASCIIEncoding.UTF8.GetByteCount(textonorm);
    byte[] phrase = Encoding.ASCII.GetBytes(textonorm);

    foreach (byte b in phrase)
    {
        textobin += Convert.ToString(b, 2);
        textval = textobin.Length;
    }

    if (textval < 64)
    {
        quant = 64 - textval;
        foreach (byte b in phrase)
        {
            textobin1 += Convert.ToString(b, 2);
        }
        textocod = textobin1.PadLeft(quant, '0');
        txtvalor = textobin1.Length;
        Console.WriteLine(textocod.ToString() + "\r\n " + "\r\n " + txtvalor.ToString());

        string[] textarray = textocod.Split('0', '1');
        int[,]  matrizval = new int[8,8];

        foreach (var substr in textarray)
        {
            Console.WriteLine(Convert.ToString());
        }
    }
    else
    {
        Console.WriteLine("ok");
    }
}

1 个答案:

答案 0 :(得分:1)

如果你准确地解释了你对它的期望,可以改进代码,但是要将值复制到矩阵中:

using System;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string textonorm, textobin = "", textobin1 = "", textocod = "";
            int textval, quant, result, txtvalor;

            Console.WriteLine("Digite a frase que deseja criptografar!");
            textonorm = Console.ReadLine();

            textval = System.Text.ASCIIEncoding.UTF8.GetByteCount(textonorm);
            byte[] phrase = Encoding.ASCII.GetBytes(textonorm);

            foreach (byte b in phrase)
            {
                textobin += Convert.ToString(b, 2);
                textval = textobin.Length;
            }

            if (textval < 64)
            {
                quant = 64;
                foreach (byte b in phrase)
                {
                    textobin1 += Convert.ToString(b, 2);
                }
                textocod = textobin1.PadLeft(quant, '0');
                txtvalor = textobin1.Length;
                Console.WriteLine(textocod.ToString() + "\r\n " + "\r\n " + txtvalor.ToString());

                char[] chararray = textocod.ToCharArray();
                int[,] matrizval = new int[8, 8];

                if (chararray.Length == 64)
                    for (int i = 0; i < 8; ++i)
                    {
                        for (int j = 0; j < 8; ++j)
                        {
                            int val = chararray[i * 8 + j] - '0';
                            Console.Write(val);
                            matrizval[i, j] = val;
                        }
                        Console.WriteLine();
                    }
            }
            else
            {
                Console.WriteLine("ok");
            }

            Console.Write("\nPress any key...");
            Console.ReadKey();
        }
    }
}