1、新建一个控制台项目
![C# char[] 转换为 byte[]](https://exp-picture.cdn.bcebos.com/3d002dbad341037df15c90c2a9bc7dc5ce672dc3.jpg)
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、写allocpubl坡纠课柩ic 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[]](https://exp-picture.cdn.bcebos.com/e076d77622bc7dc5db0c72eb5e460596b91429c3.jpg)
5、打开txt
![C# char[] 转换为 byte[]](https://exp-picture.cdn.bcebos.com/2a1ecb460596b814edbbecb743d246fe464e22c3.jpg)