欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#将数字转换成字节数组的方法

程序员文章站 2024-01-27 21:33:28
本文实例讲述了c#将数字转换成字节数组的方法。分享给大家供大家参考。具体实现方法如下: 下面的代码用到了memorystream 和 binarywriter...

本文实例讲述了c#将数字转换成字节数组的方法。分享给大家供大家参考。具体实现方法如下:

下面的代码用到了memorystream 和 binarywriter

// create a byte array from a decimal
public static byte[] decimaltobytearray (decimal src) {
 // create a memorystream as a buffer to hold the binary data
 using (memorystream stream = new memorystream()) {
  // create a binarywriter to write binary data to the stream
  using (binarywriter writer = new binarywriter(stream)) {
   // write the decimal to the binarywriter/memorystream
   writer.write(src);
   // return the byte representation of the decimal
   return stream.toarray();
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。