public final class SerialComUtil extends Object
Provides common utility functions for serial port communication related projects.
| Constructor and Description |
|---|
SerialComUtil()
Allocates a new SerialComUtil object.
|
| Modifier and Type | Method and Description |
|---|---|
static String |
byteArrayToHexString(byte[] data,
String separator)
This method creates hex string from byte array.
|
static String |
byteToHexString(byte num)
Converts the given byte value to hex string.
|
static int |
byteToUnsignedInt(byte data)
Converts the given byte's value to an unsigned integer number.
|
static long |
byteToUnsignedLong(byte data)
Converts the given byte's value to an unsigned long number.
|
static byte[] |
concat(byte[] dataA,
byte[] dataB)
Appends given byte array to another given byte array and return newly constructed
byte array.
|
static String |
decodeBCD(short bcd)
Converts a binary-coded decimal number into decimal number string.
|
static byte[] |
hexStringToByteArray(String hexStringData)
Converts given string in hexa-decimal representation to equivalent byte array.
|
static long |
hexStrToLongNumber(String hexNumStr)
This converts a number represented in hex string to decimal number.
|
static byte |
highBytefromShort(short data)
Extract and returns the high byte from the short type number passed.
|
static byte[] |
intToByteArray(int data)
Converts the given integer value to a byte type array in Little endian order.
|
static String |
intToHexString(int num)
Converts the given integer value to hex string.
|
static long |
intToUnsignedLong(int data)
Converts the given integer value to an unsigned long number.
|
static String |
longToHexString(long num)
Converts the given long value to hex string.
|
static byte |
lowByteFromShort(short data)
Extract and returns the low byte from the short type number passed.
|
static byte[] |
shortToByteArray(short data)
Converts the given short value to a byte type array in Little endian order.
|
static String |
shortToHexString(short num)
Converts the given short value to hex string.
|
static int |
shortToUnsignedInt(short data)
Converts the given short's value to an unsigned integer number.
|
static long |
shortToUnsignedLong(short data)
Converts the given short's value to an unsigned long number.
|
public static String byteArrayToHexString(byte[] data, String separator)
This method creates hex string from byte array. This is useful in bluetooth low energy applications where characteristics returned are to be interpreted or for example Internet of things applications where sensor data is getting exchanged.
data - byte array to be converted into string.separator - to be inserted after each hex value.IllegalArgumentException - if data is null.public static byte[] hexStringToByteArray(String hexStringData)
Converts given string in hexa-decimal representation to equivalent byte array.
hexStringData - string in hexa-decimal format to be converted into equivalent byte array.IllegalArgumentException - if hexStringData is null.public static String decodeBCD(short bcd)
Converts a binary-coded decimal number into decimal number string. The decimal point will be placed between 2nd and third digit of the result returned. This can be used for example to decode USB specification release number which is encoded in BCD format.
bcd - binary-coded decimal to decode.public static int byteToUnsignedInt(byte data)
Converts the given byte's value to an unsigned integer number. The least significant byte (8 bits) of the integer number will be identical to the byte (8 bits) provided, and the most significant 3 bytes (24 bits) of the integer will be zero.
data - the byte to convert.public static long byteToUnsignedLong(byte data)
Converts the given byte's value to an unsigned long number. The least significant byte (8 bits) of the long number will be identical to the byte (8 bits) provided, and the most significant 7 bytes (56 bits) of the long will be zero.
data - the byte to convert.public static byte lowByteFromShort(short data)
Extract and returns the low byte from the short type number passed.
data - the short value from whom low byte is to be extracted.public static byte highBytefromShort(short data)
Extract and returns the high byte from the short type number passed.
data - the short value from whom high byte is to be extracted.public static int shortToUnsignedInt(short data)
Converts the given short's value to an unsigned integer number. The least significant 2 byte (16 bits) of the integer number will be identical to the least significant 2 byte (16 bits) of the short number and the most significant 2 bytes (16 bits) of the integer will be zero.
data - the short type value to convert.public static long shortToUnsignedLong(short data)
Converts the given short's value to an unsigned long number. The least significant 2 byte (16 bits) of the long number will be identical to the least significant 2 byte (16 bits) of the short number and the most significant 6 bytes (48 bits) of the long number will be zero.
data - the short type value to convert.public static byte[] shortToByteArray(short data)
Converts the given short value to a byte type array in Little endian order.
data - the short type value to convert.public static long intToUnsignedLong(int data)
Converts the given integer value to an unsigned long number. The least significant 4 bytes (32 bits) of the long number will be identical to the least significant 4 bytes (32 bits) of the integer number and the most significant 4 bytes (32 bits) of the long number will be zero.
data - the int type value to convert.public static byte[] intToByteArray(int data)
Converts the given integer value to a byte type array in Little endian order.
data - the short type value to convert.public static long hexStrToLongNumber(String hexNumStr)
This converts a number represented in hex string to decimal number. It parses the string argument as a signed long with radix as 16.
hexNumStr - hex-string to be converted.NumberFormatException - if the given hex string can not be converted into numerical
representation.public static String longToHexString(long num)
Converts the given long value to hex string.
num - the long type value to convert.public static String intToHexString(int num)
Converts the given integer value to hex string.
num - the integer type value to convert.public static String shortToHexString(short num)
Converts the given short value to hex string.
num - the short type value to convert.public static String byteToHexString(byte num)
Converts the given byte value to hex string.
num - the byte type value to convert.public static byte[] concat(byte[] dataA,
byte[] dataB)
Appends given byte array to another given byte array and return newly constructed byte array.
dataA - one of the array that need to be added first.dataB - array that will be appended to array represented by dataA.Copyright © 2016. All rights reserved.