Csharp/C Sharp/File Stream/Memory Stream — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
Create a MemoryStream
using System;
using System.IO;
class MemStreamApp
{
static void Main(string[] args)
{
MemoryStream m = new MemoryStream(64);
Console.WriteLine("Length: {0}\tPosition: {1}\tCapacity: {2}",m.Length, m.Position, m.Capacity);
for (int i = 0; i < 64; i++)
{
m.WriteByte((byte)i);
}
Console.WriteLine("Length: {0}\tPosition: {1}\tCapacity: {2}",m.Length, m.Position, m.Capacity);
byte[] ba = m.GetBuffer();
foreach (byte b in ba)
{
Console.Write("{0,-3}", b);
}
m.Close();
}
}
MemoryStream.ToArray, MemoryStream.ReadDecimal
using System;
using System.IO;
class MainClass
{
public static byte[] DecimalToByteArray (decimal src){
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static decimal ByteArrayToDecimal (byte[] src){
using (MemoryStream stream = new MemoryStream(src)){
using (BinaryReader reader = new BinaryReader(stream)){
return reader.ReadDecimal();
}
}
}
public static void Main()
{
byte[] b = null;
b = BitConverter.GetBytes(true);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(BitConverter.ToBoolean(b, 0));
b = BitConverter.GetBytes(3678);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(BitConverter.ToInt32(b, 0));
b = DecimalToByteArray(285998345545.563846696m);
Console.WriteLine(BitConverter.ToString(b));
Console.WriteLine(ByteArrayToDecimal(b));
}
}
MemoryStream.Write
using System;
using System.IO;
public class MemTest {
public static void Main() {
MemoryStream memOut = new MemoryStream();
byte[] bs = { 1, 2, 3, 4, 5, 6 };
memOut.Write(bs, 0, bs.Length);
memOut.Seek(+3, SeekOrigin.Begin);
byte b = (byte)memOut.ReadByte();
Console.WriteLine("Value: " + b);
}
}
Use MemoryStream and BinaryReader to convert Byte array to decimal
using System;
using System.IO;
class Test {
public static byte[] DecimalToByteArray (decimal src) {
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static decimal ByteArrayToDecimal (byte[] src) {
using (MemoryStream stream = new MemoryStream(src)) {
using (BinaryReader reader = new BinaryReader(stream)) {
return reader.ReadDecimal();
}
}
}
public static void Main() {
byte[] b = BitConverter.GetBytes(true);
// Convert a decimal to a byte array and display.
b = DecimalToByteArray(12345678987654321.563846696m);
Console.WriteLine(BitConverter.ToString(b));
// Convert a byte array to a decimal and display.
Console.WriteLine(ByteArrayToDecimal(b));
}
}
Use MemoryStream and BinaryWriter to convert decimal to byte array
using System;
using System.IO;
class Test {
// Create a byte array from a decimal.
public static byte[] DecimalToByteArray (decimal src) {
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)){
writer.Write(src);
return stream.ToArray();
}
}
}
public static void Main() {
byte[] b = DecimalToByteArray(285998345545.563846696m);
// Convert a decimal to a byte array and display.
Console.WriteLine(BitConverter.ToString(b));
}
}