如何把string转换为char[]以及byte[],两种代码都要

解决方案 »

  1.   

    string str = "abcd";
    char [] ch = str.ToCharArray();foreach(char temp in ch)
    {
    Console.WriteLine(temp);
    }
      

  2.   

    字符串转byte数组,前提是能转换成功
      

  3.   

    string str = "abcd";
    byte[] a=new byte[0];
    a=(byte[])str;
    不知道行不行,没测试的
      

  4.   

    string s = "sdf";
                byte[] a = new UTF8Encoding(true).GetBytes(s);
    这个可以不
      

  5.   

    如何把char[]强制转换为byte[]?
      

  6.   

    我用FileStream.Write来写文件,就写字符,但FileStream.Write这个函数的第一个参数是byte[], 如何转换?
      

  7.   

    我用FileStream.Write来写文件,就写字符,但FileStream.Write这个函数的第一个参数是byte[], 如何转换?=========
    1。
    字符串转换为字节数组,你需要指定编码,不同的编码,输出的字节数组是不一样的,
    同时,读取的时候,也要使用对应的编码,否则非拉丁字符会乱出现乱码string str = "hello world";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            char[] chars = System.Text.Encoding.UTF8.GetChars(buffer);
            chars = str.ToCharArray(); // 此方法内部使用 Unicode 编码
            // byte[] buffer = System.Text.Encoding.GetEncode("GB2312").GetBytes(str);
            //char[] chars = (char[])buffer; // CS0030: 无法将类型“byte[]”转换为“char[]”
            chars = new char[buffer.Length];
            Array.Copy(buffer, chars, buffer.Length);  // 直接拷贝隐式转换2。
    事实上,如果你进行写字符串,可以选择 StreamWriterstring str = "hello world";
            using(System.IO.FileStream fstream = new System.IO.FileStream("C:\\somefile")) {
            System.IO.StreamWriter writer = new System.IO.StreamWriter(fstream);
            writer.Write(str);
    }
      

  8.   

    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);用了这个,编译通过
      

  9.   

    System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
    byte[] inputBytes =converter.GetBytes(inputString);
    string  inputString = converter.GetString(inputBytes);
    Encoding.ASCII.GetString(inputBytes,0,inputBytes.Length)); Encoding.ASCII.GetByte(Encoding.ASCII.GetString();
    欢迎加入 C#学习QQ群 572453
      

  10.   

    string str = "abcd";
    char [] ch = str.ToCharArray();
    byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
      

  11.   

    string delim = "abcd ";
    char[] delimiter = delim.ToCharArray();string data  = "abcd";
    Byte[] info = new UTF8Encoding(true).GetBytes(data);这样就可以了啊.....
      

  12.   

    http://groups.csdn.net/aspnetLab
    一个学习ASPNET很不错的群组。强烈推荐大家一起来参加哦!!!
      

  13.   

    其实还可以通过字符串索引来组合 string str = "abcdef程序员";
    char[]  c = new char[str.Length];
    for(int i=0;i<str.Length;i++)
    {
    c[i] = str[i];
    }
    Response.Write(str+"<br>");
    for(int i=0;i<c.Length;i++)
    {
    Response.Write(c[i]+"<br>");
    }