C# char[] 转换为 byte[]

2025-10-16 14:41:37

1、新建一个控制台项目

C# char[] 转换为 byte[]

2、写主函数

 static void Main(string[] args)        {            string c="";            c = Console.ReadLine();            char[] p = c.ToCharArray();            byte[] pb = new byte[64];            List<byte> ls= new List<byte>();            alloc(ls, p,64);            FileStream fs = new FileStream("D:\\abc.txt", FileMode.OpenOrCreate);            BinaryWriter sw = new BinaryWriter(fs);            sw.Write(ls.ToArray());            sw.Close();            fs.Close();

}

3、写alloc

 public static int  alloc(List<byte> bytes,char []p,int block)         {            for (int i=0;i<p.Length ;i++ )            {                bytes.Add((byte)p[i]);            }                        while (bytes.Count % block != 0)             {                bytes.Add(0);            }            return bytes.Count/block;        }

4、运行测试一下

C# char[] 转换为 byte[]

5、打开txt

C# char[] 转换为 byte[]

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢