Class Convert

java.lang.Object
com.dalsemi.onewire.utils.Convert

public class Convert extends Object
Utilities for conversion between miscellaneous datatypes.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Inner class for conversion exceptions
  • Method Summary

    Modifier and Type
    Method
    Description
    static final byte[]
    toByteArray(int intVal)
    This method constructs a LSByte byte array with 4 bytes from an int.
    static final void
    toByteArray(int intVal, byte[] byteArray)
    This method constructs a LSByte byte array with 4 bytes from an int.
    static final void
    toByteArray(int intVal, byte[] byteArray, int offset, int len)
    This method constructs a LSByte byte array with specified length from an int.
    static final byte[]
    toByteArray(long longVal)
    This method constructs a LSByte byte array with 8 bytes from a long.
    static final void
    toByteArray(long longVal, byte[] byteArray)
    This method constructs a LSByte byte array with 8 bytes from a long.
    static final void
    toByteArray(long longVal, byte[] byteArray, int offset, int len)
    This method constructs a LSByte byte array with specified length from a long.
    static final byte[]
    Converts a hex-encoded string into an array of bytes.
    static final int
    toByteArray(String strData, byte[] bData)
    Converts a hex-encoded string into an array of bytes.
    static final int
    toByteArray(String strData, byte[] bData, int offset, int length)
    Converts a hex-encoded string into an array of bytes.
    static final double
    toCelsius(double fahrenheitTemperature)
    Converts a temperature reading from Fahrenheit to Celsius.
    static final double
    toFahrenheit(double celsiusTemperature)
    Converts a temperature reading from Celsius to Fahrenheit.
    static final String
    toHexString(byte bValue)
    Converts a single byte into a hex-encoded string.
    static final String
    toHexString(byte[] data)
    Converts a byte array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(byte[] data, int offset, int length)
    Converts a byte array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(byte[] data, int offset, int length, String delimiter)
    Converts a byte array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(byte[] data, String delimiter)
    Converts a byte array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(char bValue)
    Converts a single character into a hex-encoded string.
    static final String
    toHexString(char[] data)
    Converts a char array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(char[] data, int offset, int length)
    Converts a byte array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(char[] data, int offset, int length, String delimiter)
    Converts a char array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(char[] data, String delimiter)
    Converts a char array into a hex-encoded String, using the provided delimiter.
    static final String
    toHexString(int iValue)
    Converts an integer into a hex-encoded string (LSByte).
    static final String
    toHexString(long lValue)
    Converts a long into a hex-encoded string (LSByte).
    static final int
    toInt(byte[] byteArray)
    This method constructs an int from a LSByte byte array of specified length.
    static final int
    toInt(byte[] byteArray, int offset, int len)
    This method constructs an int from a LSByte byte array of specified length.
    static final int
    toInt(String strData)
    Converts a hex-encoded string (LSByte) into an int.
    static final long
    toLong(byte[] byteArray)
    This method constructs a long from a LSByte byte array of specified length.
    static final long
    toLong(byte[] byteArray, int offset, int len)
    This method constructs a long from a LSByte byte array of specified length.
    static final long
    toLong(String strData)
    Converts a hex-encoded string (LSByte) into a long.
    static final String
    toString(double dubbel, int nFrac)
    Converts a double value into a string with the specified number of digits after the decimal place.
    static final String
    toString(float flote, int nFrac)
    Converts a float value into a string with the specified number of digits after the decimal place.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toFahrenheit

      public static final double toFahrenheit(double celsiusTemperature)
      Converts a temperature reading from Celsius to Fahrenheit.
      Parameters:
      celsiusTemperature - temperature value in Celsius
      Returns:
      the Fahrenheit conversion of the supplied temperature
    • toCelsius

      public static final double toCelsius(double fahrenheitTemperature)
      Converts a temperature reading from Fahrenheit to Celsius.
      Parameters:
      fahrenheitTemperature - temperature value in Fahrenheit
      Returns:
      the Celsius conversion of the supplied temperature
    • toLong

      public static final long toLong(byte[] byteArray, int offset, int len)
      This method constructs a long from a LSByte byte array of specified length.
      Parameters:
      byteArray - byte array to convert to a long (LSByte first)
      offset - byte offset into the array where to start to convert
      len - number of bytes to use to convert to a long
    • toLong

      public static final long toLong(byte[] byteArray)
      This method constructs a long from a LSByte byte array of specified length. Uses 8 bytes starting at the first index.
      Parameters:
      byteArray - byte array to convert to a long (LSByte first)
    • toByteArray

      public static final void toByteArray(long longVal, byte[] byteArray, int offset, int len)
      This method constructs a LSByte byte array with specified length from a long.
      Parameters:
      longVal - the long value to convert to a byte array.
      byteArray - LSByte first byte array, holds bytes from long
      offset - byte offset into the array
      len - number of bytes to get
    • toByteArray

      public static final void toByteArray(long longVal, byte[] byteArray)
      This method constructs a LSByte byte array with 8 bytes from a long.
      Parameters:
      longVal - the long value to convert to a byte array.
      byteArray - LSByte first byte array, holds bytes from long
    • toByteArray

      public static final byte[] toByteArray(long longVal)
      This method constructs a LSByte byte array with 8 bytes from a long.
      Parameters:
      longVal - the long value to convert to a byte array.
    • toInt

      public static final int toInt(byte[] byteArray, int offset, int len)
      This method constructs an int from a LSByte byte array of specified length.
      Parameters:
      byteArray - byte array to convert to an int (LSByte first)
      offset - byte offset into the array where to start to convert
      len - number of bytes to use to convert to an int
    • toInt

      public static final int toInt(byte[] byteArray)
      This method constructs an int from a LSByte byte array of specified length. Uses 4 bytes starting at the first index.
      Parameters:
      byteArray - byte array to convert to an int (LSByte first)
    • toByteArray

      public static final void toByteArray(int intVal, byte[] byteArray, int offset, int len)
      This method constructs a LSByte byte array with specified length from an int.
      Parameters:
      intVal - the int value to convert to a byte array.
      byteArray - LSByte first byte array, holds bytes from int
      offset - byte offset into the array
      len - number of bytes to get
    • toByteArray

      public static final void toByteArray(int intVal, byte[] byteArray)
      This method constructs a LSByte byte array with 4 bytes from an int.
      Parameters:
      intVal - the int value to convert to a byte array.
      byteArray - LSByte first byte array, holds bytes from long
    • toByteArray

      public static final byte[] toByteArray(int intVal)
      This method constructs a LSByte byte array with 4 bytes from an int.
      Parameters:
      longVal - the long value to convert to a byte array.
    • toByteArray

      public static final byte[] toByteArray(String strData) throws Convert.ConvertException

      Converts a hex-encoded string into an array of bytes.

      To illustrate the rules for parsing, the following String:
      "FF 0 1234 567"
      becomes:
      byte[]{0xFF,0x00,0x12,0x34,0x56,0x07}

      Parameters:
      strData - hex-encoded numerical string
      Returns:
      byte[] the decoded bytes
      Throws:
      Convert.ConvertException
    • toByteArray

      public static final int toByteArray(String strData, byte[] bData) throws Convert.ConvertException

      Converts a hex-encoded string into an array of bytes.

      To illustrate the rules for parsing, the following String:
      "FF 0 1234 567"
      becomes:
      byte[]{0xFF,0x00,0x12,0x34,0x56,0x07}

      Parameters:
      strData - hex-encoded numerical string
      bData - byte[] which will hold the decoded bytes
      Returns:
      The number of bytes converted
      Throws:
      Convert.ConvertException
    • toByteArray

      public static final int toByteArray(String strData, byte[] bData, int offset, int length) throws Convert.ConvertException

      Converts a hex-encoded string into an array of bytes.

      To illustrate the rules for parsing, the following String:
      "FF 0 1234 567"
      becomes:
      byte[]{0xFF,0x00,0x12,0x34,0x56,0x07}

      Parameters:
      strData - hex-encoded numerical string
      bData - byte[] which will hold the decoded bytes
      offset - the offset into bData to start placing bytes
      length - the maximum number of bytes to convert
      Returns:
      The number of bytes converted
      Throws:
      Convert.ConvertException
    • toHexString

      public static final String toHexString(byte[] data)
      Converts a byte array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The byte[] to convert to a hex-encoded string
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(byte[] data, int offset, int length)
      Converts a byte array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The byte[] to convert to a hex-encoded string
      offset - the offset to start converting bytes
      length - the number of bytes to convert
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(byte[] data, String delimiter)
      Converts a byte array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The byte[] to convert to a hex-encoded string
      delimiter - the delimiter to place between each byte of data
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(byte[] data, int offset, int length, String delimiter)
      Converts a byte array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The byte[] to convert to a hex-encoded string
      offset - the offset to start converting bytes
      length - the number of bytes to convert
      delimiter - the delimiter to place between each byte of data
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(byte bValue)

      Converts a single byte into a hex-encoded string.

      Parameters:
      bValue - the byte to encode
      Returns:
      String Hex-encoded String
    • toHexString

      public static final String toHexString(char[] data)
      Converts a char array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The char[] to convert to a hex-encoded string
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(char[] data, int offset, int length)
      Converts a byte array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The char[] to convert to a hex-encoded string
      offset - the offset to start converting bytes
      length - the number of bytes to convert
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(char[] data, String delimiter)
      Converts a char array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The char[] to convert to a hex-encoded string
      delimiter - the delimiter to place between each byte of data
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(char[] data, int offset, int length, String delimiter)
      Converts a char array into a hex-encoded String, using the provided delimiter.
      Parameters:
      data - The char[] to convert to a hex-encoded string
      offset - the offset to start converting bytes
      length - the number of bytes to convert
      delimiter - the delimiter to place between each byte of data
      Returns:
      Hex-encoded String
    • toHexString

      public static final String toHexString(char bValue)

      Converts a single character into a hex-encoded string.

      Parameters:
      bValue - the byte to encode
      Returns:
      String Hex-encoded String
    • toLong

      public static final long toLong(String strData) throws Convert.ConvertException

      Converts a hex-encoded string (LSByte) into a long.

      To illustrate the rules for parsing, the following String:
      "FF 0 1234 567 12 03"
      becomes:
      long 0x03120756341200ff

      Parameters:
      strData - hex-encoded numerical string
      Returns:
      the decoded long
      Throws:
      Convert.ConvertException
    • toHexString

      public static final String toHexString(long lValue)

      Converts a long into a hex-encoded string (LSByte).

      Parameters:
      lValue - the long integer to encode
      Returns:
      String Hex-encoded String
    • toInt

      public static final int toInt(String strData) throws Convert.ConvertException

      Converts a hex-encoded string (LSByte) into an int.

      To illustrate the rules for parsing, the following String:
      "FF 0 1234 567 12 03"
      becomes:
      long 0x03120756341200ff

      Parameters:
      strData - hex-encoded numerical string
      Returns:
      the decoded int
      Throws:
      Convert.ConvertException
    • toHexString

      public static final String toHexString(int iValue)

      Converts an integer into a hex-encoded string (LSByte).

      Parameters:
      iValue - the integer to encode
      Returns:
      String Hex-encoded String
    • toString

      public static final String toString(double dubbel, int nFrac)

      Converts a double value into a string with the specified number of digits after the decimal place.

      Parameters:
      dubbel - the double value to convert to a string
      nFrac - the number of digits to display after the decimal point
      Returns:
      String representation of the double value with the specified number of digits after the decimal place.
    • toString

      public static final String toString(float flote, int nFrac)

      Converts a float value into a string with the specified number of digits after the decimal place.

      Note: this function does not properly handle special case float values such as Infinity and NaN.

      Parameters:
      flote - the float value to convert to a string
      nFrac - the number of digits to display after the decimal point
      Returns:
      String representation of the float value with the specified number of digits after the decimal place.