views:

205

answers:

3

Hello,

I am using on the client C# where I am converting double values to byte array.

I am using java on the server and I am using writeDouble and readDouble to convert double values to byte arrays.

The problem is the double values from java at the end are not the double values at the begin giving to c#

writeDouble in Java Converts the double argument to a long using the doubleToLongBits method , and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.

DoubleToLongBits Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.

The Program on the server is waiting of 64-102-112-0-0-0-0-0 from C# to convert it to 1700.0 but he si becoming 0000014415464 from c# after c# converted 1700.0

this is my code in c#:

 class User
  {
    double workingStatus;
public void persist()
   {
     byte[] dataByte;
   using (MemoryStream ms = new MemoryStream())
     {
       using (BinaryWriter bw = new BinaryWriter(ms))
       {

         bw.Write(workingStatus);
         bw.Flush();
         bw.Close();
       }

    dataByte = ms.ToArray();

       for (int j = 0; j < dataByte.Length; j++)
       {

         Console.Write(dataByte[j]);
       }
}
public double WorkingStatus
      {
     get { return workingStatus; }
     set { workingStatus = value; }
      }

}
class Test
{
  static void Main()
  {
    User user = new User();
     user.WorkingStatus = 1700.0;
     user.persist();
  }

thank you for the help.

+2  A: 

If the Endianess does not match between the Systems you will have to reverse it. For Double there are no built-in methods in .Net. So you have to do it yourself:

Array.Reverse(dataByte);
doubleValue = BitConverter.ToDouble(dataByte);

BitConverter also has a IsLittleEndian property that you can use to check if you really have different endianess.

Foxfire
A: 

I tried with the Miscellaneous Utility Lilibrary wiht easyer values of int but I am not having the waiting results.

When I try to convert the int 1700 This is my result: 164600 from the c# code and java is waiting of 006-92 (this is the result of writeInt from java) java use writeInt(int) in this case. writeInt writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4 .

Can you help?

 using System;
    using System.IO;
    using System.Collections;
    using MiscUtil.IO;
    using MiscUtil.Conversion;

    namespace Project1
    {
      class User
      {


        int workingStatusInt;

       public void persist()
       {
         byte[] dataByte;
        using (MemoryStream ms = new MemoryStream())
         {
           using (EndianBinaryWriter bw = new EndianBinaryWriter(EndianBitConverter.Big, ms))
           {
             bw.Write(workingStatusInt);
             bw.Flush();
             bw.Close();
           }

           dataByte = ms.ToArray();
           Array.Reverse(dataByte);
           for (int j = 0; j < dataByte.Length; j++)
           {

             Console.Write(dataByte[j]);
           }
         } 
        }

       public int WorkingStatusInt
       {
         get { return workingStatusInt; }
         set { workingStatusInt = value; }
       }
    }

     class Test
    {

      static void Main()
      {
        User user = new User();
        user.WorkingStatusInt=1700;
        user.persist();

      }
    } //Test

}//name space
Haythem
A: 

John gives me the solution.

Java is signed from -127 to 127 and C# from 0 to 255. It works wenn I use (sbyte)byte for my values in the array

Haythem