Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert integer to hex in little endien format

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 7 '09
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]

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 7 '09

re: Convert integer to hex in little endien format


What have you done so far?
Newbie
 
Join Date: Oct 2009
Posts: 4
#3: Oct 7 '09

re: Convert integer to hex in little endien format


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

ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(9995);
System.out.println("x = "+ bb.toString());

i no have idea to print the bb..
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 7 '09

re: Convert integer to hex in little endien format


Did you want to make the println

Expand|Select|Wrap|Line Numbers
  1. System.out.println("x = " + Arrays.toString(bb.array()));
instead?
Newbie
 
Join Date: Oct 2009
Posts: 4
#5: Oct 7 '09

re: Convert integer to hex in little endien format


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
Expand|Select|Wrap|Line Numbers
  1.         ByteBuffer  bb = ByteBuffer.allocate(4);
  2.         bb.order(ByteOrder.LITTLE_ENDIAN);
  3.         bb.putInt(9995);
  4.         //bb.putInt(43690);
  5.         String xx = Arrays.toString(bb.array());
  6.  
  7.         String yy;
  8.         yy = xx.substring(1,3);
  9.         int i = Integer.parseInt(yy);
  10.         String hex1 = Integer.toHexString(i);
  11.         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 :)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Oct 7 '09

re: Convert integer to hex in little endien format


You want to try something like
Expand|Select|Wrap|Line Numbers
  1. System.out.print("x = ");
  2. for (byte b : bb.array()) {
  3.     System.out.printf("%2s", Integer.toHexString(b) + " ");
  4. }
  5.  
Read all about those formatters in the API specs for the Fomatter class.
Newbie
 
Join Date: Oct 2009
Posts: 4
#7: Oct 7 '09

re: Convert integer to hex in little endien format


very2 works thanks...
Reply

Tags
convert, little endian