There are methods Integer.toHexString()
and Long.toHexString()
. For some reason they didn't implement Short.toHexString()
.
What is the canonical method converting Short to a hex string?
It's not possible to use Integer.toHexString()
because Integer.toHexString(-33)
equals ffffffdf
which is not a short value.
ToHexString(Byte[], Int32, Int32)Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters. Parameters specify the subset as an offset in the input array and the number of elements in the array to convert.
toHexString() is a built-in function in Java which returns a string representation of the integer argument as an unsigned integer in base 16. The function accepts a single parameter as an argument in Integer data-type.
Hexadecimal Number String. The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system and are a popular choice for representing long binary values because their format is quite compact and much easier to understand compared to the long binary strings of 1's and 0's.
hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.
If in your system short
is represented as 16Bit you can also simply do the following.
String hex = Integer.toHexString(-33 & 0xffff);
Yes, you can simply take the two least-significant bytes.
This is a basic feature of the Two's Complement representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With