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

本文关键字:插入 数组元素 读取 | 更新日期: 2023-09-27 18:14:39

我有一个二进制值的数组,我将其展开并插入到数组中,但现在我想将这些元素移动到[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");
    }
}

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

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

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();
        }
    }
}