Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short toHexString

Tags:

java

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.

like image 492
Oleg Pavliv Avatar asked Nov 13 '12 07:11

Oleg Pavliv


People also ask

What does ToHexString do?

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.

How does ToHexString work Java?

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.

What is hex string?

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.

How do you convert int to hex in python?

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.


2 Answers

If in your system short is represented as 16Bit you can also simply do the following.

String hex = Integer.toHexString(-33 & 0xffff);
like image 140
David J Avatar answered Oct 13 '22 08:10

David J


Yes, you can simply take the two least-significant bytes.

This is a basic feature of the Two's Complement representation.

like image 34
emesx Avatar answered Oct 13 '22 10:10

emesx