C#数字转字节数组类BitConverter教案资料.docVIP

  • 10
  • 0
  • 约3.08千字
  • 约 4页
  • 2020-04-20 发布于浙江
  • 举报

C#数字转字节数组类BitConverter教案资料.doc

C#数字转字节数组类BitConverter BitConverter用于基础数据类型与字节数组相互转换 在vs2005中,新建控制台应用程序TestBitConvert,测试静态类BitConverter的使用情况。 ★源代码: using System; using System.Collections.Generic; using System.Text; namespace TestBitConvert { class Program { static void Main(string[] args) { Console.WriteLine(boolean占1个字节); bool[] bl = new bool[] { true, false }; for (int j = 0; j bl.Length; j++) { byte[] bufferC = BitConverter.GetBytes(bl[j]); string s = string.Empty; for (int i = 0; i bufferC.Length; i++) { s += (s.Length == 0 ? : ,) + bufferC[i]; } Console.WriteLine([{0}]转换为字节数组为:{1}, bl[j], s); } Console.WriteLine(); Console.WriteLine(字符转换为2个字节.(C#中字符时unicode编码,占2个字节)); char[] ch = new char[] { A, 我 }; for (int j = 0; j ch.Length; j++) { byte[] bufferC = BitConverter.GetBytes(ch[j]); string s = string.Empty; for (int i = 0; i bufferC.Length; i++) { s += (s.Length == 0 ? : ,) + bufferC[i]; } Console.WriteLine(字符[{0}]转换为字节数组为:{1}, ch[j], s); } Console.WriteLine(); Console.WriteLine(double类型转换为8个字节); double[] dl = new double[] { 21.3, 12.345, 1.0, 8, 1.59 }; for (int j = 0; j dl.Length; j++) { byte[] bufferC = BitConverter.GetBytes(dl[j]); string s = string.Empty; for (int i = 0; i bufferC.Length; i++) { s += (s.Length == 0 ? : ,) + bufferC[i]; } Console.WriteLine([{0}]转换为字节数组为:{1}, dl[j], s); } Console.WriteLine(); Console.WriteLine(下面测试int与unit的字节数组的问题,测试低位在前和高位在前情况:); Console.WriteLin

文档评论(0)

1亿VIP精品文档

相关文档