C# char[] 转换为 byte[]

时间:2024-10-12 13:57:47

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、写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[]

5、打开txt

C# char[] 转换为 byte[]
© 2025 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com