How to convert binary to string?

后端 未结 3 1502
野性不改
野性不改 2020-12-21 05:49
static List ConvertTextToBinary(int number, int Base)
{
    List list = new List();
    while (number!=0)
    {
        list.Add(num         


        
相关标签:
3条回答
  • 2020-12-21 06:16
    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
    }
    
    0 讨论(0)
  • 2020-12-21 06:18

    for convert byte[] to string

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

    or

    string strData = Encoding.Default.GetString(bytes); 
    
    0 讨论(0)
  • 2020-12-21 06:23

    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.

    0 讨论(0)
提交回复
热议问题