开发者

CIL and JVM Little endian to big endian in c# and java

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 ar开发者_C百科e 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜