1

i m new to java. i want to convert a byte array of decimal value to hexadecimal string. my input byte array is [0, 0, 0, 0, 0, 0, 1, -28]. i m getting 00000000000001e4 instead of 0000001e4. plz help me to solve this problem

 public static String ConvetToHex(byte[] decValue) 
{

    String value = "";
    for(int i = 0;i<decValue.length;i++)
    {
         value = value+ Integer.toString((decValue[i] & 0xff) + 0x100, 16).substring(1);
    }
    return value;
}
0

1 Answer 1

2

It looks correct to me. Eight bytes should turn into 16 hex characters. You can use

return new BigInteger(1, decValue).toString(16);

but it will produce the same output.

Sign up to request clarification or add additional context in comments.

6 Comments

but is there anyway to get in the specified format..?
Sure you can but you will produce a number which is meaningless as there is no way to read the value correctly.
For example in the format you suggest there is no way to tell if 1001 is 10, 01 (as it should be) or 10, 0, 1 or 1,0,0,1.
i wrote the same code in .NET like value = value + Convert.ToInt32(decvalue[i]).toString("x"); i got the output which i mentioned.
You can write the code in Java too. Doesn't change the fact that the format is broken.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.