2016-03-20 6 views
0

文字列を2進数に変換するプログラムを構築しています(I.E. "A" = 01000001)。そして、ユーザが望むなら、その2進数でASCII文字に戻すことができます。ここでは、コード: C言語で数値を追加する方法

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

namespace NDR_011 
{ 
class Program 
{ 

    public static Byte[] BinStr(String binary) 
    { 
     var list = new List<Byte>(); 

     for (int i = 0; i < binary.Length; i += 8) 
     { 
      String t = binary.Substring(i, 8); 

      list.Add(Convert.ToByte(t, 2)); 
     } 

     return list.ToArray(); 
    } 

    public static void Print(object mess) 
    { 
     string tmp = mess.ToString().ToUpper(); 
     Console.Write(tmp); 
    } 

    private static List<string> buffer = new List<string>(); 
    private static string outfile = "C:/tmp/bytes.bin"; 
    static void Main(string[] args) 
    { 
     string tmp = ""; 
     Print("NDR 011\n"); 
     while (true) 
     { 
      Print(""); tmp = Console.ReadKey().Key.ToString().ToUpper(); 
      if (Console.CursorLeft > 0) 
      { 
       Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); 
      } 
      if (tmp == ConsoleKey.F1.ToString()) 
      { 
       break; 
      } else if (tmp == ConsoleKey.F2.ToString()) 
      { 
       comp(); 

       continue; 
      } else if (tmp == ConsoleKey.F4.ToString()) 
      { 
       buffer.Clear(); 

       continue; 
      } else if (tmp == ConsoleKey.F5.ToString()) 
      { 
       Print("N "); string a = Console.ReadLine(); 
       outfile = a; 

       continue; 
      } else if (tmp == ConsoleKey.F5.ToString()) 
      { 
       outfile = "C:/tmp/bytes.bin"; 
       Print("Out file reset\n"); 

       continue; 
      } else if (tmp == ConsoleKey.F7.ToString()) 
      { 
       //Print("N "); // string a = Console.ReadLine(); 
       string a = "C:/tmp/bytes.bin"; 
       string[] s = File.ReadAllText(a).Split(' '); 
       char[] end = new char[s.Length - 1]; 
       for (int i=0;i<end.Length;i++) 
       { 
        end[i] = (char)BinStr(s[i])[0]; 
        //Print(end[i]); 
       } 
       //Print((char)BinStr(s[0])[0]); 
       if (end[0] == 'A' && end[1] == 'D' && end[2] == 'D') 
       { 
        for (int i=0+3;i<end.Length;i++) 
        { 
         int n = end[i] + end[i]; 
         Print(n); 
        } 
       } 
       //decompile(a); 

       continue; 
      } 
      while (tmp.Length > 1) 
      { 
       char a = tmp[tmp.Length - 1]; 
       tmp = a.ToString(); 
      } 
      buffer.Add(tmp); 
     } 
    } 
    static void comp() 
    { 
     if (buffer == null || buffer.Count <= 0) 
     { 
      Print("Error buffer empty"); 
      return; 
     } 
     char[] r = new char[buffer.Count]; 
     for (int i=0;i<buffer.Count;i++) 
     { 
      r[i] = Convert.ToChar(buffer[i]); 
      Print(r[i]); 
     } 
     foreach (char ch in r) 
     { 
      string a = Convert.ToString((int)ch, 2); 
      while (a.Length != 8) 
      { 
       string b = "0"; 
       a = b + a; 
      } 
      File.AppendAllText(outfile, a + " "); 
     } 
     Print("Compile done!\n"); 
    } 
    static void decompile(string filename) 
    { 

    } 
    static void run() 
    { 

    } 
} 
} 

は(私はこの記事に追加したときにindenationが台無しGOT)

問題はこれです:私は、私は乱数を取得するファイルから取得した値を追加しようとすると、 100102のような、そしてそのような奇妙なもの。私は間違って何をしていますか?おかげ

+0

組み込みのクラス/構造体を使用することはできませんか? – Ian

+0

デバッガで何を見つけたのですか?どこが最初に狂ったのですか? – HABO

+0

C#では 'string'と' char'に対してASCIIを使用していません。あなたの関数が[C0 Controls and Basic Latin](http://www.unicode.org/charts/nameslist/index.html)の文字ブロックでのみ動作する場合、入力がその要件を満たさない場合は例外をスローします。 –

答えて

0

は、ここでは、1と0のバイナリ文字列に文字列を変換する方法は次のとおりです。文字列にその背中を変換するには

var binstring = string.Join(" ", Encoding.ASCII.GetBytes(("Welcome, World!")).Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0'))); 

、我々はパースバイトにする必要があります[]メソッドで、それから配列

public static byte[] GetBytes(string s) 
     { 
      byte[] result = new byte[(s.Length + 7)/8]; 

      int i = 0; 
      int j = 0; 
      foreach (char c in s) 
      { 
       result[i] <<= 1; 
       if (c == '1') 
        result[i] |= 1; 
       j++; 
       if (j == 8) 
       { 
        i++; 
        j = 0; 
       } 
      } 
      return result; 
     } 

はその後、我々はそれらのバイトを取得し、我々は、単に使用して文字列に変換することができます:

Encoding.ASCII.GetString(GetBytes(binstring)); 
そうのように参照先:
How could I encode a string of 1s and 0s for transport?
How to get the binary code behind ASCII (C#)
+0

ありがとうございました! – Programmer2120

関連する問題