Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c# - How to convert binary to string?

static List<int> ConvertTextToBinary(int number, int Base)
{
    List<int> list = new List<int>();
    while (number!=0)
    {
        list.Add(number % Base);
        number = number / Base;
    }
    list.Reverse();
    return list;
}



static void Main(string[] args)
{

   string s = "stackoverflow";
   int counter=0;
   while (counter!=s.Length)
   {
       int[] a = ConvertTextToBinary(s[counter], 2).ToArray();
       for (int i = 0; i < a.Length; i++)
       {
           Console.Write(a[i]);
       }
       Console.Write("
");
       counter++;
   }
}

I wrote a method to convert string to binary, its working fine. But now I want to convert binary to string eg: 1101000 is equal to h.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

for convert byte[] to string

byte[] bytes ;
string base64Data = Convert.ToBase64String (bytes);

or

string strData = Encoding.Default.GetString(bytes); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...