2016-10-23 2 views
-3

Ich habe ein Array mit binären Werten, das habe ich explodiert und in ein Array eingefügt, aber jetzt möchte ich diese Elemente in eine [8, 8] -Matrix verschieben Linie), wie kann ich das tun?Array-Elemente in einer Matrix lesen/einfügen

Ich habe versucht, aber ich konnte etwas Gutes Beispiel nicht gefunden

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

Antwort

1

Der Code könnte verbessert werden, wenn Sie genau erklären, was Sie von ihm erwarten, aber die Werte in die Matrix zu kopieren:

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