当前位置: 移动技术网 > IT编程>开发语言>c# > C#将数字转换成字节数组的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网