C#將數(shù)字轉換成字節(jié)數(shù)組的方法
更新時間:2015年04月04日 12:39:13 作者:令狐不聰
這篇文章主要介紹了C#將數(shù)字轉換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#將數(shù)字轉換成字節(jié)數(shù)組的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
下面的代碼用到了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#程序設計有所幫助。
相關文章
C# 實現(xiàn)SDL2進行視頻播放窗口截圖和字幕添加
這篇文章主要介紹了C# 實現(xiàn)SDL2進行視頻播放窗口截圖和字幕添加,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

