none
CIL and JVM Little endian to big endian RRS feed

  • Question

  • Hello,

    I have String und Double Data converted to array of Bytes with c# and now I would like to read the data with a Java Program.

    I compared the Array of Bytes that c# gives and the Array of Bytes that a java Program will gives with the same variable values and they are not the same.

    How can I tell C# convert data in array bytes with a way that a java program can correctly read it?

    Is it a problem of little endian and big endian?.

     

    Thank you for the help

    Haythem

    Friday, April 23, 2010 4:36 PM

Answers

  • I understood! I thought the "-" you were writing were separators. They are minus signs!

    The bytes you have in C# are binary equal to the ones in Java. The difference is that C# uses unsigned bytes.

    Next time, try to separate the bytes so that the minus signs appear as such.

    • Marked as answer by Haythem77 Tuesday, April 27, 2010 1:19 PM
    Tuesday, April 27, 2010 11:48 AM

All replies

  • Since your data contains strings, it can be also encoding problem, .NET supports lots of encodings like Ascii, UTF8, UTF16, UTF16BigEndian, etc. You have to check what encoding the Java application expects and use that encoding to generate the array of bytes.
    Saturday, April 24, 2010 12:20 PM
  • Thank you for your reply.

    The problem that I have are occured when I write double and int in an bytearray. When I read this byte array on Java and convertet them to double and int I will have other value as in the beginn.

     

    I think I need a converter from littleendian to bigendian. Or a class in c# that helps me to convert double to double bigendian.

    Monday, April 26, 2010 8:15 AM
  • Converting Endians is simply reversing the byte array like this:

    static void ConvertEndian(byte[] byteArray, int offset, int numberOfBytes)
    {
      byte temp;
      for (int i = 0; i < numberOfBytes / 2; i++)
      {
        temp = byteArray[i];
        byteArray[i] = byteArray[byteArray.Length - 1 - i];
        byteArray[byteArray.Length - 1 - i] = temp;
      }
    }

    offset is where your integer data start in the array, and number of Bytes is 4 for int and 8 for double.

    Usage:

    int x = 5;
    byte[] byteArray = BitConverter.GetBytes(x);
    ConvertEndian(byteArray, 0, 4);
    Monday, April 26, 2010 9:53 AM
  • it works für int with ConvertEndian(byteArray, 0, 4) for int.

    How can I get Double values?, ConvertEndian(byteArray, 0 , 8) ist not working for double values.

    Can you please tell my more about the offset importance.

     

    Monday, April 26, 2010 12:07 PM
  • I wrote the code without using offset, sorry about that. I intended offset to be used for converting part of the array instead of the whole array. For exampe if you have a 12 bytes array with the first 4 bytes representing an int and the last 8 bytes representing a double, to convert the double bytes, you call ConvertEndian(byteArray, 4, 8) (will convert bytes from 4 to 11)

    Here is the code corrected to use offset.

    static void ConvertEndian(byte[] byteArray, int offset, int numberOfBytes)
    {
     byte temp;
     for (int i = 0; i < numberOfBytes / 2; i++)
     {
      temp = byteArray[i + offset];
      byteArray[i + offset] = byteArray[numberOfBytes - 1 - i + offset];
      byteArray[numberOfBytes - 1 - i + offset] = temp;
     }
    }
    
    Monday, April 26, 2010 12:40 PM
  • ok.

    I think I know where is the problem.

    I am trying to write in C# the same method as writeDouble(double) in Java. But this is writing Long values to byte array.

    writeDouble (double v)

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

     

    Convert.Tolong(double) is not giving  in c#.

    Any solution?

    Monday, April 26, 2010 12:56 PM
  • use thsi method in BitConverter class: BitConverter.DoubleToInt64Bits
    Monday, April 26, 2010 1:02 PM
  • I am not comming to find the write form.

    I writed the double value 1700.0 in an array and I have this result on java

     

    64-102-11200000 

     

    und this result in c#

     0000014415464

     

    I don t know why they are not the same. How can I do it in C# that they have the same formatter?

    I think the problem is that Java is using a IEEE 754-2008 float format and C# an other.

     

    with this form:

     (byte)(0xff & (v >> 56))
    (byte)(0xff & (v >> 48))
    (byte)(0xff & (v >> 40))
    (byte)(0xff & (v >> 32))
    (byte)(0xff & (v >> 24))
    (byte)(0xff & (v >> 16))
    (byte)(0xff & (v >> 8))
    (byte)(0xff & v)

     

    Monday, April 26, 2010 3:13 PM
  • C# uses correctly the IEEE 754 format.

    64-102-112-0-0-0-0-0 is the IEEE 754 format of 179.5

    If it was encoded with the IEEE 754-2008 decimal encoding, it think it would be 4.7e+35.

    I don't know where you got those bytes, but something is wrong.

    You can test it here: http://babbage.cs.qc.cuny.edu/IEEE-754/

    Monday, April 26, 2010 5:10 PM
  • I tried on my code with this values and it gives other values.

     

    this is my code

    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();
      }

    This code should give me 64-102-112-0-0-0-0-0 to let me happy.

     

    Can you see the error?

     

    Monday, April 26, 2010 5:44 PM
  • The error is thinking it should give those bytes. Where do they come from?
    Monday, April 26, 2010 5:49 PM
  • They come from a JVM where I used writeDouble(double) and readDouble.

    writeDouble 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.

    This Program is on server and waiting of 64-102-112-0-0-0-0-0 from C# on the client to convert it to 1700.0

    Tuesday, April 27, 2010 6:14 AM
  • 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?

    This is my code

    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
    

    Tuesday, April 27, 2010 10:17 AM
  • I understood! I thought the "-" you were writing were separators. They are minus signs!

    The bytes you have in C# are binary equal to the ones in Java. The difference is that C# uses unsigned bytes.

    Next time, try to separate the bytes so that the minus signs appear as such.

    • Marked as answer by Haythem77 Tuesday, April 27, 2010 1:19 PM
    Tuesday, April 27, 2010 11:48 AM
  • Yes it true.

    I used (sbyte)byte for my array and it works.

    java is signed, from -127 until 127 and C# is unsigned from 0 to 255

     

    Thank you

    Tuesday, April 27, 2010 1:21 PM