Package org.symbol.sdk.utils
Class Converter
java.lang.Object
org.symbol.sdk.utils.Converter
- Converter
- Hex / integer / byte conversion helpers.
-
Method Summary
Modifier and TypeMethodDescriptionstatic longbytesToInt(byte[] input, int size) Converts little-endian bytes to alongfor any size (1, 2, 4 or 8); a size-8 u64>= 2^63reads back negative.static longbytesToInt(byte[] input, int size, boolean isSigned) Converts bytes to alongfor any size (1, 2, 4 or 8); a size-8 u64>= 2^63reads back negative.static longbytesToInt(byte[] input, int offset, int size, boolean isSigned) Converts bytes to alongfor any size (1, 2, 4 or 8); a size-8 u64>= 2^63reads back negative.static byte[]hexToUint8(String input) Converts a hex string to a byte array.static byte[]intToBytes(long value, int byteSize) Converts an integer to bytes by truncating its 64-bit two's-complement encoding tobyteSize.static booleanisHexString(String input) Determines whether a string is a hex string.static intConverts a fixed-width integral wrapper to a 32-bitintbit pattern, accepting the signedintrange and the full unsigned 32-bit range (a magnitude>= 2^31yields the negative two's-complement pattern).static intParses a string to a 32-bitintbit pattern, the 32-bit analogue oftoLong(String): a0x-prefixed hex string or a non-negative decimal is read across the full unsigned 32-bit range (a value>= 2^31yields the negative two's-complement pattern), while a--prefixed decimal is parsed signed.static longConverts a fixed-width integral wrapper (Integer/Long/Short/Byte) or an unsigned-64-bit-rangeBigIntegerto a 64-bitlong; any other type is rejected.static longParses a string to a 64-bitlongbit pattern: a0x/0X-prefixed hex string or a non-negative decimal is read across the full unsigned range (a value>= 2^63yields the negative two's-complement pattern), while a--prefixed decimal is parsed signed — so, liketoLong(Number), negatives are accepted.static Stringuint8ToHex(byte[] input) Converts a byte array to a hex string.
-
Method Details
-
isHexString
- Converter.isHexString
- Determines whether a string is a hex string.
- Parameters:
input- String to test.- Returns:
trueif the input is a hex string,falseotherwise.
-
hexToUint8
- Converter.hexToUint8
- Converts a hex string to a byte array.
- Parameters:
input- Hex encoded string.- Returns:
- Byte array corresponding to the input.
-
uint8ToHex
- Converter.uint8ToHex
- Converts a byte array to a hex string.
- Parameters:
input- Byte array.- Returns:
- Hex encoded string corresponding to the input.
-
bytesToInt
public static long bytesToInt(byte[] input, int size, boolean isSigned) - Converter.bytesToInt
- Converts bytes to a
longfor any size (1, 2, 4 or 8); a size-8 u64>= 2^63reads back negative.
- Parameters:
input- Byte array.size- Number of bytes (1, 2, 4 or 8).isSigned-trueif a value narrower than 8 bytes should be sign-extended.- Returns:
- Value corresponding to the input.
-
bytesToInt
public static long bytesToInt(byte[] input, int offset, int size, boolean isSigned) - Converter.bytesToInt
- Converts bytes to a
longfor any size (1, 2, 4 or 8); a size-8 u64>= 2^63reads back negative.
- Parameters:
input- Byte array.offset- Offset into the array at which to start reading.size- Number of bytes (1, 2, 4 or 8).isSigned-trueif a value narrower than 8 bytes should be sign-extended.- Returns:
- Value corresponding to the input.
-
bytesToInt
public static long bytesToInt(byte[] input, int size) - Converter.bytesToInt
- Converts little-endian bytes to a
longfor any size (1, 2, 4 or 8); a size-8 u64>= 2^63reads back negative.
- Parameters:
input- Byte array.size- Number of bytes (1, 2, 4 or 8).- Returns:
- Unsigned value.
-
intToBytes
public static byte[] intToBytes(long value, int byteSize) - Converter.intToBytes
- Converts an integer to bytes by truncating its 64-bit two's-complement encoding to
byteSize.
- Parameters:
value- Integer value.byteSize- Number of output bytes.- Returns:
- Byte representation of the integer.
-
toLong
- Converter.toLong
- Parses a string to a 64-bit
longbit pattern: a0x/0X-prefixed hex string or a non-negative decimal is read across the full unsigned range (a value>= 2^63yields the negative two's-complement pattern), while a--prefixed decimal is parsed signed — so, liketoLong(Number), negatives are accepted.
- Parameters:
value- String value (signed/unsigned decimal, or0x-prefixed hexadecimal).- Returns:
- Parsed value as a 64-bit two's-complement bit pattern.
- Throws:
IllegalArgumentException- If the string is not a valid integer that fits a 64-bit pattern.
-
toLong
- Converter.toLong
- Converts a fixed-width integral wrapper (
Integer/Long/Short/Byte) or an unsigned-64-bit-rangeBigIntegerto a 64-bitlong; any other type is rejected.
- Parameters:
value- Integral descriptor number.- Returns:
- Converted value as a 64-bit two's-complement bit pattern.
- Throws:
IllegalArgumentException- If the value is not an integral number fitting a 64-bit pattern.
-
toInt
- Converter.toInt
- Converts a fixed-width integral wrapper to a 32-bit
intbit pattern, accepting the signedintrange and the full unsigned 32-bit range (a magnitude>= 2^31yields the negative two's-complement pattern). Anything outside[Integer.MIN_VALUE, 2^32 - 1]is rejected rather than truncated.
- Parameters:
value- Integral descriptor number.- Returns:
- Converted value as a 32-bit two's-complement bit pattern.
- Throws:
IllegalArgumentException- If the value does not fit a 32-bit pattern (or is not an integral wrapper).
-
toInt
- Converter.toInt
- Parses a string to a 32-bit
intbit pattern, the 32-bit analogue oftoLong(String): a0x-prefixed hex string or a non-negative decimal is read across the full unsigned 32-bit range (a value>= 2^31yields the negative two's-complement pattern), while a--prefixed decimal is parsed signed. Anything outside the unsigned 32-bit range is rejected rather than truncated.
- Parameters:
value- String value (signed/unsigned decimal, or0x-prefixed hexadecimal).- Returns:
- Parsed value as a 32-bit two's-complement bit pattern.
- Throws:
IllegalArgumentException- If the string is unparseable or does not fit a 32-bit pattern.
-