Tuesday, 15 June 2010

java - How to convert minus hex value to String and byte array -



java - How to convert minus hex value to String and byte array -

i want convert decimal -10 value hex in string , byte array format.

i have tried

string minhex = integer.tohexstring(integer.valueof(-10)); system.out.println(minhex);

which results in fffffff6, , think not correct. , converting byte array using below function found

convert string representation of hex dump byte array using java?

public static byte[] hexstringtobytearray(string s) { int len = s.length(); byte[] info = new byte[len / 2]; (int = 0; < len; += 2) { data[i / 2] = (byte) ((character.digit(s.charat(i), 16) << 4) + character.digit(s.charat(i+1), 16)); } homecoming data; }

so not sure work minus hex value or not.

to convert hex string way expect i.e. -10 converted -a, use:

string hex = integer.tostring(-10, 16);

to convert byte array, convert int byte array, represented same way:

byte[] bytes = bytebuffer.allocate(4).putint(-10).array();

java string hex

No comments:

Post a Comment