Convert double[] to byte[]

G

Guest

I found the following way to convert from double[] to byte[] and I wonder if there is an easier one

And I have another question I can see from the value in canFloat if my Data can be stored in Float Data
If I want to do this and only convert 4 of the bytes in the double wich are the right ones? high or low

Thanks in Advanc

Marti

Guid FileName = Guid.NewGuid()
String SaveAs = "E:\\Stacks\\" + FileName.ToString()
File.SaveAs(SaveAs)

FileIO myIO = new Dickenbestimmung.FileIO()
int[] resolution
float[] length
resolution = new int[] {0,0,0,0}
length = new float[] {0,0,0,0 }

int canFloat = myIO.dbl_read(SaveAs,ref resolution,ref length)
int maxindex = resolution[0]*resolution[1]*resolution[2]*resolution[3]

byte[] ByteData = new byte[8*maxindex]
byte[] actDbl = new byte[8]

for (int i = 0; i<maxindex;i++

actDbl = BitConverter.GetBytes(myIO.data)
for (int p = 0; p<8;p++) ByteData[(8*i)+p]=actDbl[p]


object[] obj = new object[5];
obj[0] = ByteData
obj[1] = Name.Text
obj[2] = SaveAs
obj[3] = FileName
obj[4] = resolution[0]

SqlHelper.ExecuteNonQuery(connectionString,"sp_add_stack",obj);
 
N

Nicholas Paldino [.NET/C# MVP]

Chucker,

The easiest way would be to use the static BlockCopy method on the
Buffer class. I've responded to your earlier post on this as well.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chucker said:
I found the following way to convert from double[] to byte[] and I wonder if there is an easier one!

And I have another question I can see from the value in canFloat if my
Data can be stored in Float Data.
If I want to do this and only convert 4 of the bytes in the double wich
are the right ones? high or low?
Thanks in Advance

Martin

Guid FileName = Guid.NewGuid();
String SaveAs = "E:\\Stacks\\" + FileName.ToString();
File.SaveAs(SaveAs);

FileIO myIO = new Dickenbestimmung.FileIO();
int[] resolution;
float[] length;
resolution = new int[] {0,0,0,0};
length = new float[] {0,0,0,0 };

int canFloat = myIO.dbl_read(SaveAs,ref resolution,ref length);
int maxindex = resolution[0]*resolution[1]*resolution[2]*resolution[3];

byte[] ByteData = new byte[8*maxindex];
byte[] actDbl = new byte[8];

for (int i = 0; i<maxindex;i++)
{
actDbl = BitConverter.GetBytes(myIO.data);
for (int p = 0; p<8;p++) ByteData[(8*i)+p]=actDbl[p];
}

object[] obj = new object[5];
obj[0] = ByteData;
obj[1] = Name.Text;
obj[2] = SaveAs;
obj[3] = FileName;
obj[4] = resolution[0];

SqlHelper.ExecuteNonQuery(connectionString,"sp_add_stack",obj);
 
B

BillT

You might also be able to use a technique with a struct
used as a union, and LayoutKind.Explicit. I haven't tried
this with arrays, but it would be easy for you to try.

The cleanest solution is to use the explicit memory layout
to obtain a union:

[StructLayout(LayoutKind.Explicit)]
public struct Union {
[FieldOffset(0)]
public int iv;
[FieldOffset(0)]
public float fv;
}
....
Union u = new Union();
u.iv = v;
u.fv; // float value

[Credits: Sample copied from
http://mailserver.di.unipi.it/pipermail/dotnet/msg00152.htm
l ]
-----Original Message-----
I found the following way to convert from double[] to byte
[] and I wonder if there is an easier one!
And I have another question I can see from the value in
canFloat if my Data can be stored in Float Data.
If I want to do this and only convert 4 of the bytes in
the double wich are the right ones? high or low?
Thanks in Advance

Martin

Guid FileName = Guid.NewGuid();
String SaveAs = "E:\\Stacks\\" + FileName.ToString();
File.SaveAs(SaveAs);

FileIO myIO = new Dickenbestimmung.FileIO();
int[] resolution;
float[] length;
resolution = new int[] {0,0,0,0};
length = new float[] {0,0,0,0 };

int canFloat = myIO.dbl_read(SaveAs,ref resolution,ref length);
int maxindex = resolution[0]*resolution[1]*resolution[2] *resolution[3];

byte[] ByteData = new byte[8*maxindex];
byte[] actDbl = new byte[8];

for (int i = 0; i<maxindex;i++)
{
actDbl = BitConverter.GetBytes(myIO.data);
for (int p = 0; p<8;p++) ByteData[(8*i)+p]=actDbl[p];
}

object[] obj = new object[5];
obj[0] = ByteData;
obj[1] = Name.Text;
obj[2] = SaveAs;
obj[3] = FileName;
obj[4] = resolution[0];

SqlHelper.ExecuteNonQuery (connectionString,"sp_add_stack",obj);
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top