Package com.google.common.primitives
Class UnsignedBytes
- java.lang.Object
-
- com.google.common.primitives.UnsignedBytes
-
@GwtIncompatible public final class UnsignedBytes extends java.lang.Object
Static utility methods pertaining tobyteprimitives that interpret values as unsigned (that is, any negative valuebis treated as the positive value256 + b). The corresponding methods that treat the values as signed are found inSignedBytes, and the methods for which signedness is not an issue are inBytes.See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
- Author:
- Kevin Bourrillion, Martin Buchholz, Hiroshi Yamauchi, Louis Wasserman
-
-
Field Summary
Fields Modifier and Type Field Description static byteMAX_POWER_OF_TWOThe largest power of two that can be represented as an unsignedbyte.static byteMAX_VALUEThe largest value that fits into an unsigned byte.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static bytecheckedCast(long value)Returns thebytevalue that, when treated as unsigned, is equal tovalue, if possible.static intcompare(byte a, byte b)Compares the two specifiedbytevalues, treating them as unsigned values between 0 and 255 inclusive.static java.lang.Stringjoin(java.lang.String separator, byte... array)Returns a string containing the suppliedbytevalues separated byseparator.static java.util.Comparator<byte[]>lexicographicalComparator()Returns a comparator that compares twobytearrays lexicographically.static bytemax(byte... array)Returns the greatest value present inarray, treating values as unsigned.static bytemin(byte... array)Returns the least value present inarray, treating values as unsigned.static byteparseUnsignedByte(java.lang.String string)Returns the unsignedbytevalue represented by the given decimal string.static byteparseUnsignedByte(java.lang.String string, int radix)Returns the unsignedbytevalue represented by a string with the given radix.static bytesaturatedCast(long value)Returns thebytevalue that, when treated as unsigned, is nearest in value tovalue.static voidsort(byte[] array)Sorts the array, treating its elements as unsigned bytes.static voidsort(byte[] array, int fromIndex, int toIndex)Sorts the array betweenfromIndexinclusive andtoIndexexclusive, treating its elements as unsigned bytes.static voidsortDescending(byte[] array)Sorts the elements ofarrayin descending order, interpreting them as unsigned 8-bit integers.static voidsortDescending(byte[] array, int fromIndex, int toIndex)Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order, interpreting them as unsigned 8-bit integers.static inttoInt(byte value)Returns the value of the given byte as an integer, when treated as unsigned.static java.lang.StringtoString(byte x)Returns a string representation of x, where x is treated as unsigned.static java.lang.StringtoString(byte x, int radix)Returns a string representation ofxfor the given radix, wherexis treated as unsigned.
-
-
-
Field Detail
-
MAX_POWER_OF_TWO
public static final byte MAX_POWER_OF_TWO
The largest power of two that can be represented as an unsignedbyte.- Since:
- 10.0
- See Also:
- Constant Field Values
-
MAX_VALUE
public static final byte MAX_VALUE
The largest value that fits into an unsigned byte.- Since:
- 13.0
- See Also:
- Constant Field Values
-
-
Method Detail
-
toInt
public static int toInt(byte value)
Returns the value of the given byte as an integer, when treated as unsigned. That is, returnsvalue + 256ifvalueis negative;valueitself otherwise.Java 8 users: use
Byte.toUnsignedInt(byte)instead.- Since:
- 6.0
-
checkedCast
@CanIgnoreReturnValue public static byte checkedCast(long value)
Returns thebytevalue that, when treated as unsigned, is equal tovalue, if possible.- Parameters:
value- a value between 0 and 255 inclusive- Returns:
- the
bytevalue that, when treated as unsigned, equalsvalue - Throws:
java.lang.IllegalArgumentException- ifvalueis negative or greater than 255
-
saturatedCast
public static byte saturatedCast(long value)
Returns thebytevalue that, when treated as unsigned, is nearest in value tovalue.- Parameters:
value- anylongvalue- Returns:
(byte) 255ifvalue >= 255,(byte) 0ifvalue <= 0, andvaluecast tobyteotherwise
-
compare
public static int compare(byte a, byte b)
Compares the two specifiedbytevalues, treating them as unsigned values between 0 and 255 inclusive. For example,(byte) -127is considered greater than(byte) 127because it is seen as having the value of positive129.- Parameters:
a- the firstbyteto compareb- the secondbyteto compare- Returns:
- a negative value if
ais less thanb; a positive value ifais greater thanb; or zero if they are equal
-
min
public static byte min(byte... array)
Returns the least value present inarray, treating values as unsigned.- Parameters:
array- a nonempty array ofbytevalues- Returns:
- the value present in
arraythat is less than or equal to every other value in the array according tocompare(byte, byte) - Throws:
java.lang.IllegalArgumentException- ifarrayis empty
-
max
public static byte max(byte... array)
Returns the greatest value present inarray, treating values as unsigned.- Parameters:
array- a nonempty array ofbytevalues- Returns:
- the value present in
arraythat is greater than or equal to every other value in the array according tocompare(byte, byte) - Throws:
java.lang.IllegalArgumentException- ifarrayis empty
-
toString
public static java.lang.String toString(byte x)
Returns a string representation of x, where x is treated as unsigned.- Since:
- 13.0
-
toString
public static java.lang.String toString(byte x, int radix)
Returns a string representation ofxfor the given radix, wherexis treated as unsigned.- Parameters:
x- the value to convert to a string.radix- the radix to use while working withx- Throws:
java.lang.IllegalArgumentException- ifradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX.- Since:
- 13.0
-
parseUnsignedByte
@CanIgnoreReturnValue public static byte parseUnsignedByte(java.lang.String string)
Returns the unsignedbytevalue represented by the given decimal string.- Throws:
java.lang.NumberFormatException- if the string does not contain a valid unsignedbytevaluejava.lang.NullPointerException- ifstringis null (in contrast toByte.parseByte(String))- Since:
- 13.0
-
parseUnsignedByte
@CanIgnoreReturnValue public static byte parseUnsignedByte(java.lang.String string, int radix)
Returns the unsignedbytevalue represented by a string with the given radix.- Parameters:
string- the string containing the unsignedbyterepresentation to be parsed.radix- the radix to use while parsingstring- Throws:
java.lang.NumberFormatException- if the string does not contain a valid unsignedbytewith the given radix, or ifradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX.java.lang.NullPointerException- ifstringis null (in contrast toByte.parseByte(String))- Since:
- 13.0
-
join
public static java.lang.String join(java.lang.String separator, byte... array)
Returns a string containing the suppliedbytevalues separated byseparator. For example,join(":", (byte) 1, (byte) 2, (byte) 255)returns the string"1:2:255".- Parameters:
separator- the text that should appear between consecutive values in the resulting string (but not at the start or end)array- an array ofbytevalues, possibly empty
-
lexicographicalComparator
public static java.util.Comparator<byte[]> lexicographicalComparator()
Returns a comparator that compares twobytearrays lexicographically. That is, it compares, usingcompare(byte, byte)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example,[] < [0x01] < [0x01, 0x7F] < [0x01, 0x80] < [0x02]. Values are treated as unsigned.The returned comparator is inconsistent with
Object.equals(Object)(since arrays support only identity equality), but it is consistent withArrays.equals(byte[], byte[]).- Since:
- 2.0
-
sort
public static void sort(byte[] array)
Sorts the array, treating its elements as unsigned bytes.- Since:
- 23.1
-
sort
public static void sort(byte[] array, int fromIndex, int toIndex)
Sorts the array betweenfromIndexinclusive andtoIndexexclusive, treating its elements as unsigned bytes.- Since:
- 23.1
-
sortDescending
public static void sortDescending(byte[] array)
Sorts the elements ofarrayin descending order, interpreting them as unsigned 8-bit integers.- Since:
- 23.1
-
sortDescending
public static void sortDescending(byte[] array, int fromIndex, int toIndex)
Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order, interpreting them as unsigned 8-bit integers.- Since:
- 23.1
-
-