Results 1 to 4 of 4
  1. #1
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default How do I convert a Big-Endian(64 bit) to Little Endian(64 bit) in Java.

    Hi,

    How do I convert a big-endian(64 bit) to little endian(64 bit) in Java and vice versa.

    For e.g. I have 40429C28F5C28F5C in big endian, I want to convert it into little endian (5C8FC2F5289C4240) and vice versa.


    How would I code a function to do this in Java?

    Thanks.

    Sukhpreet Kaur

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    10

    Default

    This topic interests me as it brings me back to my Assembly programming days... check out here which may hint at a solution...

    Something like the following may be helpful and was taken from here:
    Java Code:
    [FONT=tahoma,arial,sans-serif][SIZE=-1][COLOR=#000000]import java.nio.ByteOrder;
    
    public class Endian {
      public static void main(String argv[]) {
        ByteOrder b = ByteOrder.nativeOrder();
        if (b.equals(ByteOrder.BIG_ENDIAN)) { 
          System.out.println("Big-endian");
        } else {
          System.out.println("Little-endian");
        }
      }
    }
    [/COLOR][/SIZE][/FONT]


    Another solution is found here, although doesn't look 64-bit...

    Let us know your final result.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    Hi CaptainMorgan,

    Thank you for your help and the links you've posted.

    I am looking for something Java CodeGuru: Deal with big-endian and little-endian order
    but this example code is for 4-bytes, I am looking something for 8 bytes(64-bits) where I can enter (40429C28F5C28F5C) as an argument and the function can return (5C8FC2F5289C4240) ...

    Any suggestions/help will be appreciated.

    Thank you.

  4. #4
    SKaur is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    The code is shown below:

    [/code]

    public static long bigToLittleEndian(long bigendian) {
    ByteBuffer buf = ByteBuffer.allocate(8);

    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(bigendian);

    buf.order(ByteOrder.LITTLE_ENDIAN);
    return buf.getLong(0);
    }


    public static void main(String[] args) {
    long res = bigToLittleEndian(0x40429C28F5C28F5CL);
    System.out.println(Long.toHexString(res));
    }

    [/code]

    Thank you.

Similar Threads

  1. Convert .java to .exe
    By susan in forum New To Java
    Replies: 6
    Last Post: 02-11-2009, 07:47 AM
  2. convert html to text using java
    By praveen@asia-mail.com in forum New To Java
    Replies: 1
    Last Post: 11-14-2007, 03:08 PM
  3. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM
  4. Can't convert java.lang.String to int.
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-13-2007, 05:05 PM
  5. convert VB6 applications to Java
    By bbq in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •