如题顺便嗯 一直很糊涂一个问题数据流都是二进制?每次读8个字符应该怎么定义?“8位”是几个字符?

解决方案 »

  1.   

    “8位”是几个字符?  与编码有关,ASCII 8位 1个字节  汉字 16位 2个字节从二进制流读出 byte[]把byte[]转化成字符串string str = Encoding.ASCII.GetString(ArryByte);
    string str = Encoding.UTF8.GetString(ArryByte);
    string str = Encoding.Default.GetString(ArryByte);
      

  2.   

    1楼已经很清楚的解释了问题读完数据流获取到byte[]再根据编码情况调用Encoding.xxx.GetString()获取字符串
      

  3.   

    问题是你从什么源读取二进制数组? 另 8位=1字节 似乎不分汉字非汉字吧
    一般放到MemoryStream里read就行了
      

  4.   


            private void button8_Click(object sender, EventArgs e)
            {
                byte[] a = Encoding.Unicode.GetBytes("中华人民共和国");
                MessageBox.Show(Encoding.Unicode.GetString(a));
            }
      

  5.   

    using System;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using System.Text.RegularExpressions;
    using System.Text;class Program
    {
        static void Main(string[] args)
        {
            byte[] a = Encoding.Unicode.GetBytes("中华人民共和国");
            foreach(byte ss in a)
                Console.WriteLine(ss);
            a[0] = 1;
            foreach (byte ss in a)
                Console.WriteLine(ss);
        }
    }
    这样?
      

  6.   


    private void button1_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream(@"C:\temp.txt", FileMode.Open, FileAccess.Read);
        byte[] ArryByte = new byte[fs.Length];
        fs.Read(ArryByte, 0, 1024);                   // 读1KB
        string str = Encoding.UTF8.GetString(ArryByte);
        MessageBox.Show(str);
    }
      

  7.   


            private void button8_Click(object sender, EventArgs e)
            {
                byte[] a = Encoding.Unicode.GetBytes("中华人民共和国");
                for (int i = 0; i < 7; i++)
                {
                    MessageBox.Show(Encoding.Unicode.GetString(a, i*2, 2));//每次转2个字节,因为采用unicode编码
                }
            }
      

  8.   

    FileStream..::.Read Method Reads a block of bytes from the stream and writes the data in a given buffer.Namespace:  System.IO
    Assembly:  mscorlib (in mscorlib.dll) 
    C# 
    public override int Read(
    byte[] array,
    int offset,
    int count
    )
     Parameters
    array
    Type: array<System..::.Byte>[]()[]When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source. offset
    Type: System..::.Int32The byte offset in array at which the read bytes will be placed. count
    Type: System..::.Int32The maximum number of bytes to read. Return Value
    Type: System..::.Int32The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.
      

  9.   

    例子:
    http://www.yoda.arachsys.com/csharp/readbinary.html
    http://devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=400
      

  10.   

    算了,给楼主从头科普。
    首先计算机一律以二进制保存数据,也就是说你的硬盘从本质上说记录的就是一大堆的0和1。而为了处理起来方便,规定对这些0和1的处理一律以8位为单位,也就是8位二进制数,从0-255。这也就是所谓的“字节”,在C#中就定义为一个byte变量。平常我们说一个文件有2k,就是说这个文件包含有2x1024个8位数字的数据量。
    至于具体的每一个byte代表什么含义,也就是二进制数的编码方式,完全是写和读的人自己约定的。比如你完全可以规定0代表a,1代表b,以此类推。一般英文只有26字母,算上大写也只有52个,还可以表达一大堆奇怪的字符。而汉字就比较多,一般用2个字节表达一个汉字。
    楼主完全可以自定义一套编码规则,但是显然其他人不会承认的,所以一般使用的还是通用的编码规则,一般常用的有ANSI,UniCode,Utf8等。C#也给出了基于各种编码规则byte和字符串相互转换的方法,就是前面各位老大给出的
      

  11.   

    private void button8_Click(object sender, EventArgs e)
            {
                byte[] a = Encoding.Unicode.GetBytes("中华人民共和国");
                for (int i = 0; i < 7; i++)
                {
                    MessageBox.Show(Encoding.Unicode.GetString(a, i*2, 2));//每次转2个字节,因为采用unicode编码
                }
            }