convert double[] to byte??

is this possible i need to convert an array of double to byte so that i can put it in a database..

thank you!


Answer this question

convert double[] to byte??

  • Lighthouse Builder

    Your database doesn't have a floating point data type Most should.


  • Pranav Garg

    Does this make sense, i'm not sure if it's exactly what you are asking

     

    double[] testInts = new double[2];

    byte[] byteArray;

    testInts.SetValue(12,0);

    testInts.SetValue(15,1);

    byteArray = new byte[testInts.Length];

    int index = 0;

    foreach (double d in testInts)

    {

    byteArray.SetValue(Convert.ToByte(d), index);

    index = index + 1;

    }

    foreach (byte b in byteArray)

    {

    Console.WriteLine(b);

    }

    Console.ReadLine();

     

    I have a feeling it's not what you mean. It wouldn't let you convert any double > 255 to a byte so it can't be what you meant sorry.


  • jschumann.net

    Hi,

    To know about 'Memo' type , please see at http://www.functionx.com/access/Lesson10.htm and if u google , u'll get more sites.

    I think to store in MS Access , first u need to convert this double array -> byte array->Encode it to ASCII and then store it MS Access database.I'm not sure.I did it long time back.Just check with ur frnd.

    And in VC , database is programming is bit challenging.All the best.

    Thanx&Regards,
    Ch.T.Gopi Kumar.



  • Donal McWeeney

    See, what I'm trying to do is to put double[] val in a database, a friend told me to convert it to bytes and then store it in the database, but I don't know how. Anyway, what is the memo type would it be easier if i used that Thanks so much.

  • SteffoS

    Hi, uhm the OLE DB object doesn't seem to work..

  • Mark Beiley

    Hi. If you want to store a double in an access database do the following: Open the table in design view (Right click the table and select "Design View"). Set as type of the field (where you want to store the double in) to "Number" and select as file size "Double" (File size is found in the bottom of the window). That's it. Now you can store a double in it!

  • JonnyAJAX

    The BitConverter is a great deal here (the double array contains just some test values):

    double[] d = new double[] { double.MaxValue, double.MinValue, 345, 346655 };

    for (int i = 0; i < d.Length; i++)

    {

    byte[] result = BitConverter.GetBytes(d[ i ]);

    }



  • e.henriquez

    Hi ,

    As Zamial said, the max value of byte is 255 , so how is it possible to put a double array into a single byte value.

    A double array can be conveted to byte array but not to a byte value.

    U have told that u r going to store this byte value in MS-Access. Is it Byte or Memo type

    if it is memo type, i guess u can pass byte[]. or else u can format all the array values into a string having a delimeter as seperator and save it in database as text type.

    Hope my thought may help you.

    Could u please let me know ur opinion after reading these things

    cheers,

    Ch.T.Gopi Kumar.




  • mahima

    I' using MS Access. My data type for the field is Number (byte).

  • Kevin Hoffman

    Well that would make the column where the double[] are stored of type "OLE Object" (ADO.NET returns that column as byte array and allows to set a byte array!). Next my code to convert the doubles to byte[] should be used to create a byte array.

    The following two methods convert a double[] to byte[] and a byte[] back to double[]

    private static double[] ConvertByteArrayToDoubleArray(byte[] value)

    {

    if (value == null)

    throw new ArgumentNullException("value");

    // Create an array that holds the results.

    double[] result = new double[value.Length / 8];

    for(int i = 0; i < value.Length; i = i + 8)

    {

    byte[] b = new byte[ 8 ];

    // Copy the values (for the next double) of the byte array

    // to the temp array.

    Array.Copy(value, i, b, 0, b.Length);

    double d = BitConverter.ToDouble(b, 0);

    // Set the result in the result array.

    result[i / 8] = d;

    }

    return result;

    }

    private static byte[] CreateByteArrayFromDoubleArray(double[] value)

    {

    if (value == null)

    throw new ArgumentNullException("value");

    // Create an array that holds the result.

    byte[] result = new byte[value.Length * 8];

    for (int i = 0; i < value.Length; i++)

    {

    byte[] b = BitConverter.GetBytes(value[ i ]);

    // Copy the byte array to the result.

    Array.Copy(b, 0, result, (i * 8), b.Length);

    }

    return result;

    }

    The usage would be the following:

    // Some dummy input value. Could be any other double[] array.

    double[] d = new double[] { double.MaxValue, double.MinValue, 345, 346655 };

    // Convert the double array to a byte array.

    byte[] bytes = CreateByteArrayFromDoubleArray(d);

    // Convert the byte array back to a double array.

    double[] doubles = ConvertByteArrayToDoubleArray(bytes);



  • dszhou

    Thank you. I will try this right away!

  • Heino

    Hi littlle guru,

    It's not doube needs to be stored ,it's double[].

    Regards,

    Ch.T.Gopi Kumar



  • omerk76

    OLE Object is by ADO.NET returned as byte array. You need to convert the double array to a byte array and store that - by using parameters on a OleDbCommand.



  • convert double[] to byte??