Convert integer to hex in little endien format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ngajay
    New Member
    • Oct 2009
    • 4

    #1

    Convert integer to hex in little endien format

    Please tell me...
    how to convert integer to hex in little endian format ?

    example : i'm input int = 9995
    then
    the output in Array String is [0b,27,00,00]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What have you done so far?

    Comment

    • ngajay
      New Member
      • Oct 2009
      • 4

      #3
      until now i'm using bytebuffer but i can't view the result
      my code is

      ByteBuffer bb = ByteBuffer.allo cate(4);
      bb.order(ByteOr der.LITTLE_ENDI AN);
      bb.putInt(9995) ;
      System.out.prin tln("x = "+ bb.toString());

      i no have idea to print the bb..

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Did you want to make the println

        Code:
        System.out.println("x = " + Arrays.toString(bb.array()));
        instead?

        Comment

        • ngajay
          New Member
          • Oct 2009
          • 4

          #5
          thanks that's work for me...
          but the result is in decimal...
          can you give me a hint how to get the result in hex like [0b,27,00,00]

          i try code like this
          Code:
                  ByteBuffer  bb = ByteBuffer.allocate(4);
                  bb.order(ByteOrder.LITTLE_ENDIAN);
                  bb.putInt(9995);
                  //bb.putInt(43690);
                  String xx = Arrays.toString(bb.array());
          
                  String yy;
                  yy = xx.substring(1,3);
                  int i = Integer.parseInt(yy);
                  String hex1 = Integer.toHexString(i);
                  System.out.println("Hexa : " + hex1);
          the result is "Hexa : b" not "Hexa : 0B"

          the code has problem when put integer 43690(0xAAAA in hex) the result in array is [-86, -86, 0, 0] -86 when convert in java the result is ffffffAA not AA

          would you help me againt :)

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            You want to try something like
            Code:
            System.out.print("x = ");
            for (byte b : bb.array()) {
            	System.out.printf("%2s", Integer.toHexString(b) + " ");
            }
            Read all about those formatters in the API specs for the Fomatter class.

            Comment

            • ngajay
              New Member
              • Oct 2009
              • 4

              #7
              very2 works thanks...

              Comment

              Working...