4
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("\n");
       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.

nawfal
  • 70,104
  • 56
  • 326
  • 368
Desire
  • 563
  • 4
  • 13
  • 28
  • You've solved the problem by yourself, but there also is the standard libary BinaryFormatter class, which can help you a lot, both encoding and decoding.http://msdn.microsoft.com/es-es/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.80).aspx – Baltasarq Nov 26 '11 at 12:29
  • any other option without using BinaryFormatter class – Desire Nov 26 '11 at 12:35
  • 1
    Is this homework? If it is, please tag it accordingly. – Baltasarq Nov 26 '11 at 13:01
  • no its not a homework, its just a very tiny part of my large project – Desire Nov 26 '11 at 13:19

4 Answers4

9

for convert byte[] to string

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

or

string strData = Encoding.Default.GetString(bytes); 
Esi
  • 389
  • 5
  • 14
4

You can convert a single bit-set to a character as follows:

int[] h = { 1, 1, 0, 1, 0, 0, 0 };
int result = 0;
int bitValue = 1;

for (int i = h.Length - 1; i >= 0 ; i--)
{
    result += h[i] * bitValue;
    bitValue *= 2;
}

Console.WriteLine((char)result);

Each bit corresponds to a multiple of 2. By starting at the last bit and multiplying the bit value by two, you get the result you want.

Sam
  • 7,252
  • 16
  • 46
  • 65
Gigi
  • 28,163
  • 29
  • 106
  • 188
3
static string ConvertBinaryToText(List<List<int>> seq){
    return new String(seq.Select(s => (char)s.Aggregate( (a,b) => a*2+b )).ToArray());
}

static void Main(){
   string s = "stackoverflow";
   var binary = new List<List<int>>();
   for(var counter=0; counter!=s.Length; counter++){
       List<int> a = ConvertTextToBinary(s[counter], 2);
       binary.Add(a);
       foreach(var bit in a){
           Console.Write(bit);
       }
       Console.Write("\n");
   }
   string str = ConvertBinaryToText(binary);
   Console.WriteLine(str);//stackoverflow
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • List corresponds to character , List> corresponds to string in your program. Select part decompose List> to List ,and apply a function to each. Aggregate to be applied as a function taking two arguments comb fold sequence. It converts each List to character. List> -> list ... List -> char ... char -> char array -> new string(char []) -> string. – BLUEPIXY Nov 26 '11 at 22:29
0
     string nameStr = "1011010100111010";   
     var result = "";
        byte[] bytes = new byte[nameStr.Length / 8];
        for (int i = 0; i < bytes.Length; i++)
        {
            // coverting each substring to a byte
            bytes[i] = Convert.ToByte(nameStr.Substring(i * 8, 8), 2);
        }
        // coverting it back to a string
        var sb = new StringBuilder();
        foreach (var b in bytes)
        {
            sb.Append((char)b);
        }
        result = sb.ToString();

The binary must be a multiple of 8 and if it is not add 0's to the front until it is a multiple of 8.