比如一个txt文件中有32个字符全部为0或1,如何才能把这32个字符写入UInt32类型,32位0或1的二进制表示4个字节,但不知道该怎么转,请各位指点。

解决方案 »

  1.   

    2进制转换成16进制的  在赋给UINT32?
      

  2.   

    刚写的,凑合看一下:
    -------
    namespace TestSync
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "11101000"; //101010的字符串,
                Char[] chars = str.ToCharArray();
                UInt32 result = 0;            for (int i = 0; i < chars.Length; i++)
                {
                    Console.WriteLine(chars[i]);
                    result += ((UInt32)Math.Pow(2, chars.Length - 1 - i)) * Convert.ToUInt32(chars[i].ToString());
                }
                Console.WriteLine(result);
            }
        }
    }
      

  3.   


        class Program
        {
            static void Main(string[] args)
            {
                string input = "00000000000000000000000000000011";
                UInt32 output = Convert.ToUInt32(input, 2);
            }
        }
      

  4.   


        class Program
        {
            static void Main(string[] args)
            {
                string input = "00000000000000000000000000000011";
                //UInt32 output = Convert.ToUInt32(input, 2);
                UInt32 output = 0;
                foreach (char a in input.ToCharArray())
                {
                    UInt32 tmp = 0;
                    if ('0' == a)
                    {
                        tmp = 0;
                    }
                    else if ('1' == a)
                    {
                        tmp = 1;
                    }
                    else
                    {
                        //error;
                        return;
                    }                output <<= 1;
                    output |= tmp;
                }
            }
        }
      

  5.   

    先当字符串读入,然后转化为int类型,转化时指定目标数据的进制为2进制.
      

  6.   

    System.IO.StreamReader sr = new System.IO.StreamReader("C:\\1.txt");
    string bb = sr.ReadToEnd();
    uint aa=Convert.ToUInt32(bb, 2);
      

  7.   

    string str = "11101000";
    Console.WriteLine(Convert.ToUInt32(str, 2));可以直接把字符串作为二进制转换。不错。
      

  8.   

    byte[] bytes = new byte[4];
    uint u=BitConverter.ToUInt32(bytes, 0);
      

  9.   

    首先将文件中的32个字符读到字符串str中,再利用Convert.ToUInt32(str, 2)函数即可达到目的。
      

  10.   

    byte[] bytes = new byte[4];
    uint u=BitConverter.ToUInt32(bytes, 0);
      

  11.   

    BitConverter.ToUInt32具体可参考msdn