Stephan van Hulst wrote:What do you mean? What wrong with Integer.toHexString() or String.format()?
Integer.toHexString: doesn't 0-pad - if the result is 0-15 (0-F) it only returns a String with one single character - that's the main issue in the original first post hence the question why it's needed to be done manually
as toHexString is located in Integer class it suggest to work on 32bit values (also, it's only in Long, Float and Double - the smaller datatypes Byte, Short and Character doesn't provide it - also, the toString() with additional radix parameter is only provided by Integer and Long) - and as "hex string" nowdays associated with two-leter encoding of an 8-bit byte it could be expected to at least pad to strings with length of multiple of 2 - although it could be debated if a padding to a 6 charachter string should be used when the value fits in 24 bit
String.format(): when using with some like %02x it's easy to represent a value as a 0-pad 2 character hex string - but what happens internal, parsing regex, converting, etc - is very heavy overhead for something that should be simple
in addition the parseInt method in my eyes lacks support for correctly parsing negative values correctly
correctly parse and doesn't throw any exception
instead of parse to negative integer max it throws a NumberFormatException
even more surprising:
wich doesn't make sense at all correctly parses to negative max int - although all except char is signed in java - hence "negative (negativ max int)" should throw the exception as it would result in int max +1 wich doesn't fit anymore
that's what I meant with "what SE API provide doesn't provide whats expected"
sure - as long year dev one might know these quirks - but for a new one just learn java or maybe programming at all this doesn't seem to make sense - wich mostly causes such threads here wich mostly answered with "because someone decided so and made it spec > (link to spec here)" - wich, although maybe technically correct, isn't really a good explanation