添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Convert integer to hex in little endien format

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

example : i'm input int = 9995
the output in Array String is [0b,27,00,00] Oct 7 '09 # 1
6 10503
r035198x
13,262 What have you done so far? # 2
ngajay
4 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.. # 3
r035198x
13,262 Did you want to make the println

Expand | Select | Wrap | Line Numbers
  1. System.out.println("x = " + Arrays.toString(bb.array()));
instead? 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. String yy;
  7. yy = xx.substring(1,3);
  8. int i = Integer.parseInt(yy);
  9. String hex1 = Integer.toHexString(i);
  10. 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 :) # 5
r035198x
13,262 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. Read all about those formatters in the API specs for the Fomatter class. # 7
Message
Cancel Changes

Sign in to post your reply or Sign up for a free account.