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));
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
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));
}
}
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);
}
}
Create a MemoryStream
using System;
using System.IO;
class MemStreamApp
{
static void Main(string[] args)
{
MemoryStream m = new MemoryStream(64);
Console.WriteLine(“Length: {0} Position: {1} Capacity: {2}”,m.Length, m.Position, m.Capacity);
for (int i = 0; i < 64; i++) { m.WriteByte((byte)i); } Console.WriteLine("Length: {0} Position: {1} Capacity: {2}",m.Length, m.Position, m.Capacity); byte[] ba = m.GetBuffer(); foreach (byte b in ba) { Console.Write("{0,-3}", b); } m.Close(); } } [/csharp]
CurrentSize and Scope
using System;
using System.IO;
using System.IO.IsolatedStorage;
class MainClass {
static void Main(string[] args) {
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly()) {
store.CreateDirectory("MyFolder");
using (Stream fs = new IsolatedStorageFileStream("MyFile.txt", FileMode.Create, store)) {
StreamWriter w = new StreamWriter(fs);
w.WriteLine("Test");
w.Flush();
}
Console.WriteLine("Current size: " + store.CurrentSize.ToString());
Console.WriteLine("Scope: " + store.Scope.ToString());
Console.WriteLine("Contained files include:");
string[] files = store.GetFileNames("*.*");
foreach (string file in files) {
Console.WriteLine(file);
}
}
}
}
GetFileNames, CreateDirectory, GetUserStoreForAssembly
using System;
using System.IO;
using System.IO.IsolatedStorage;
class MainClass {
static void Main(string[] args) {
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly()) {
store.CreateDirectory("MyFolder");
using (Stream fs = new IsolatedStorageFileStream("MyFile.txt", FileMode.Create, store)) {
StreamWriter w = new StreamWriter(fs);
w.WriteLine("Test");
w.Flush();
}
Console.WriteLine("Current size: " + store.CurrentSize.ToString());
Console.WriteLine("Scope: " + store.Scope.ToString());
Console.WriteLine("Contained files include:");
string[] files = store.GetFileNames("*.*");
foreach (string file in files) {
Console.WriteLine(file);
}
}
}
}