Class Convert


  • public final class Convert
    extends java.lang.Object
    Convert is used to convert between objects and basic types. It also contains many other utility methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void append​(java.lang.StringBuffer sb, char c, int count)
      This method appends a number of characters into a StringBuffer.
      static java.lang.String appendPath​(java.lang.String path1, java.lang.String path2)
      Concatenates two strings ensuring there's a single slash between them.
      static java.lang.StringBuffer appendTimeStamp​(java.lang.StringBuffer sb, Time t, boolean appendDate, boolean appendTime)
      Appends the given timestamp to a StringBuffer, using the current Date format and separators.
      static java.lang.String bytesToHexString​(byte[] b)
      Converts the given sequence of bytes to a String.
      static java.lang.String bytesToHexString​(byte[] b, int off, int len)
      Converts the given sequence of bytes to a String.
      static int chars2int​(java.lang.String fourChars)
      Convert a creator id or a type to an int.
      static java.nio.charset.Charset charsetForName​(java.lang.String charsetName)
      Returns a charset object for the named charset.
      static java.lang.String[] cloneStringArray​(java.lang.String[] strs)
      Creates a copy of the given array.
      static int detectSortType​(java.lang.Object item)
      Returns the sort type for the given item sample (which is usually the first item of the array being sorted) based on: If item is not of String type, SORT_OBJECT is returned.
      static int digitOf​(char ch, int radix)
      returns the value of the digit in the specified radix.
      static int doubleToIntBits​(double f)
      Converts the given double to its bit representation in IEEE 754 format, using 4 bytes instead of 8 (a convertion to float is applied).
      static long doubleToLongBits​(double value)
      Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.
      static java.lang.String dup​(char c, int count)
      Duplicates the given char count times.
      static boolean equals​(byte[] b1, byte[] b2)  
      static void fill​(boolean[] a, int from, int to, boolean value)
      Fills the given array, within the range, with the value specified.
      static void fill​(byte[] a, int from, int to, int value)
      Fills the given array, within the range, with the value specified.
      static void fill​(char[] a, int from, int to, char value)
      Fills the given array, within the range, with the value specified.
      static void fill​(double[] a, int from, int to, double value)
      Fills the given array, within the range, with the value specified.
      static void fill​(int[] a, int from, int to, int value)
      Fills the given array, within the range, with the value specified.
      static void fill​(long[] a, int from, int to, long value)
      Fills the given array, within the range, with the value specified.
      static void fill​(short[] a, int from, int to, int value)
      Fills the given array, within the range, with the value specified.
      static void fill​(java.lang.Object[] a, int from, int to, java.lang.Object value)
      Fills the given array, within the range, with the value specified.
      static char forDigit​(int digit, int radix)
      returns the digit in the corresponding radix.
      static int getBreakPos​(FontMetrics fm, java.lang.StringBuffer sb, int start, int width, boolean doWordWrap)
      Finds the best position to break the line, with word-wrap and respecting \n.
      static byte[] getBytes​(java.lang.StringBuffer sb)
      Returns the bytes of the given StringBuffer using the current CharacterConverter instance.
      static double getDistancePoint2Rect​(int x, int y, int x1, int y1, int x2, int y2)
      Computes the distance between a point (x,y) and a rectangle (corners x1,y1 and x2,y2).
      static java.lang.String getFileName​(java.lang.String fullPath)
      Given a full path name, returns the name of the file.
      static java.lang.String getFileNameOnly​(java.lang.String fullPath)
      Given a full path name or a single file name, returns the name of the file without the extension.
      static java.lang.String getFilePath​(java.lang.String fullPath)
      Given a full path name, returns the normalized path of the file, without the slash.
      static int hashCode​(java.lang.StringBuffer sb)
      Gets the hashcode of the string being stored by this StringBuffer.
      static int hex2signed​(java.lang.String s)
      Returns an int with the conversion of the signed hexadecimal to integer.
      static int hex2unsigned​(java.lang.String s)
      Returns an int with the conversion of the unsigned hexadecimal to integer.
      static byte[] hexStringToBytes​(java.lang.String s)
      Converts the given String into an array of bytes.
      static byte[] hexStringToBytes​(java.lang.String s, boolean ignoreNonHexChars)
      Converts the given String into an array of bytes.
      static void insertAt​(java.lang.StringBuffer sb, int pos, char c)
      Inserts the given char at the position in the StringBuffer.
      static java.lang.String insertLineBreak​(int maxWidth, FontMetrics fm, java.lang.String text)
      This method is useful to insert line breaks into the text used in the MessageBox constructor.
      static java.lang.String insertLineBreakBalanced​(int maxWidth, FontMetrics fm, java.lang.String text)
      This method is useful to insert line breaks into the text used in the Toast.show; it behaves like insertLineBreak but tries to split the string in a balanced number of chars.
      static java.lang.String int2chars​(int i)
      Convert an int to a creator id or a type.
      static double intBitsToDouble​(int i)
      Converts the given IEEE 754 bit representation of a float to a double.
      static byte[] ints2bytes​(int[] from, int count)
      Converts the given int array into a byte array.
      static boolean isLowerCase​(char c)
      Returns true if the given char is in lowercase.
      static boolean isUpperCase​(char c)
      Returns true if the given char is in uppercase.
      static double longBitsToDouble​(long bits)
      Returns the double-float corresponding to a given bit represention.
      static int max​(int... ai)
      Returns the maximum value among the given ints
      static int min​(int... ai)
      Returns the minimum value among the given ints
      static java.lang.String normalizePath​(java.lang.String path)
      Normalizes a string using / as the path separator and removing any duplicate separators.
      static int numberOf​(java.lang.String s, char c)
      Returns the number of chars in a String.
      static java.lang.String numberPad​(int s, int size)
      Converts the int to String and pads it with the char 160 (that have the same width of a number) at left.
      static java.lang.String numberPad​(java.lang.String s, int size)
      Pads the given string with the char 160 (that have the same width of a number), putting it before or after the string.
      static void qsort​(java.lang.Object[] items, int first, int last)
      Applies the Quick Sort algorithm to the elements of the given array.
      static void qsort​(java.lang.Object[] items, int first, int last, int sortType)
      Applies the Quick Sort algorithm to the elements of the given array, sorting in ascending order.
      static void qsort​(java.lang.Object[] items, int first, int last, int sortType, boolean ascending)
      Applies the Quick Sort algorithm to the elements of the given array, in ascending or descending order.
      static void registerCharacterConverter​(AbstractCharacterConverter characterConverter)
      Registers the given subclass of AbstractCharacterConverter to the list of encodings supported to this instance of the running TotalCross VM
      static java.lang.String remove​(java.lang.String source, java.lang.String chars)
      Removes the given set of chars from the String.
      static java.lang.String removeAccentuation​(java.lang.String s)
      Returns the given string without accentuation characters, using the unicode range 0-255
      static java.lang.String replace​(java.lang.String source, java.lang.String from, java.lang.String to)
      Replace all occurences of the from String by the to String in the given source String.
      static long rol​(long i, int n, int bits)
      Do a rol of n bits in the given i(nt).
      static long ror​(long i, int n, int bits)
      Do a ror of n bits in the given i(nt).
      static boolean setDefaultConverter​(java.lang.String name)
      Changes the default Character Converter to the given one.
      static java.lang.String spacePad​(java.lang.String what, int count, boolean before)
      Pads the given string with spaces, putting it before or after the string.
      static int sum​(int... ai)
      Returns the sum of the given ints
      static int sum​(int[] ai, int startIndex, int endIndex)
      Returns the sum of the given ints, in the given range (startIndex <= x < endIndex).
      static java.lang.String toCurrencyString​(double d, int decimalPlaces)
      Returns the given double formatted using the Settings decimal and thousands separators.
      static java.lang.String toCurrencyString​(java.lang.String s, int decimalPlaces)
      Returns the given double formatted using the Settings decimal and thousands separators.
      static double toDouble​(java.lang.String s)
      Converts the String to a double.
      static double toDouble​(java.lang.String s, double defaultValue)
      Converts the given String to a double.
      static int toInt​(java.lang.String s)
      Converts the given String to an int.
      static int toInt​(java.lang.String s, int defaultValue)
      Converts the given String to an int.
      static java.lang.String[] tokenizeArguments​(java.lang.String arg)
      Tokenizes a command line arguments, breaking it into an array of strings.
      static java.lang.String[] tokenizeString​(java.lang.String input, char delim)
      Tokenize the given input string.
      static java.lang.String[] tokenizeString​(java.lang.String input, char... delims)
      Splits the specified string around matches of the given delimiters.
      static java.lang.String[] tokenizeString​(java.lang.String input, java.lang.String delim)
      Tokenize the given input string.
      static long toLong​(java.lang.String s)
      Converts the String to a long.
      static long toLong​(java.lang.String s, int radix)
      Converts the String to a long in the given radix.
      static char toLowerCase​(char c)
      Converts the char to lower case letter
      static short toShort​(java.lang.String s)
      Convert a string to the short type.
      static java.lang.String toString​(boolean b)
      Converts the given boolean to a String.
      static java.lang.String toString​(char c)
      Converts the given char to a String.
      static java.lang.String toString​(double d)
      Converts the given double to a String, formatting it using scientific notation.
      static java.lang.String toString​(double val, int decimalCount)
      Converts the given double to a String, formatting it with the given number of decimal places.
      static java.lang.String toString​(int i)
      Converts the given int to a String.
      static java.lang.String toString​(int i, int radix)
      Converts the int to a String in the given radix.
      static java.lang.String toString​(long l)
      Converts the long to a String at base 10.
      static java.lang.String toString​(long i, int radix)
      Converts the long to a String in the given radix.
      static java.lang.String toString​(java.lang.String doubleValue, int n)
      formats a String as a double, rounding with n decimal places.
      static java.lang.String toString4D​(long i, int radix)  
      static java.lang.String[] toStringArray​(java.lang.Object[] objs)
      Converts the given object array to a String array by calling toString in each object.
      static char toTitleCase​(char ch)
      Converts a Unicode character into its titlecase equivalent mapping.
      static char toUpperCase​(char c)
      Converts the char to upper case letter
      static java.lang.String unsigned2hex​(int b, int places)
      Returns a String with the conversion of the unsigned integer to hexadecimal, using the given number of places (up to 8).
      static java.lang.String zeroPad​(int s, int size)
      Converts the int to String and pads it with zeroes at left.
      static java.lang.String zeroPad​(java.lang.String s, int size)
      Pads the string with zeroes at left.
      static java.lang.String zeroUnpad​(java.lang.String s)
      Strips the left zeros of the String.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • CRLF_BYTES

        public static final byte[] CRLF_BYTES
      • b2h

        public static char[] b2h
      • MIN_CHAR_VALUE

        public static final char MIN_CHAR_VALUE
        The minimum char value: ''
        See Also:
        Constant Field Values
      • MAX_CHAR_VALUE

        public static final char MAX_CHAR_VALUE
        The maximum char value:
        See Also:
        Constant Field Values
      • MAX_SHORT_VALUE

        public static final short MAX_SHORT_VALUE
        The maximum short value: 32767
        See Also:
        Constant Field Values
      • MIN_SHORT_VALUE

        public static final short MIN_SHORT_VALUE
        The minimum short value: -32768
        See Also:
        Constant Field Values
      • MIN_LONG_VALUE

        public static final long MIN_LONG_VALUE
        The minimum long value: -9223372036854775808
        See Also:
        Constant Field Values
      • MAX_LONG_VALUE

        public static final long MAX_LONG_VALUE
        The maximum long value: 9223372036854775807
        See Also:
        Constant Field Values
      • MIN_INT_VALUE

        public static final int MIN_INT_VALUE
        The minimum int value: -2147483648
        See Also:
        Constant Field Values
      • MAX_INT_VALUE

        public static final int MAX_INT_VALUE
        The maximum int value: 2147483647
        See Also:
        Constant Field Values
      • MAX_DOUBLE_VALUE

        public static final double MAX_DOUBLE_VALUE
        The maximum double value: 1.7976931348623157E308d
        See Also:
        Constant Field Values
      • MIN_DOUBLE_VALUE

        public static final double MIN_DOUBLE_VALUE
        The minimum double value: 4.9E-324d
        See Also:
        Constant Field Values
      • MAX_DOUBLE_DIGITS

        public static final int MAX_DOUBLE_DIGITS
        The maximum number of digits in a double value, used when formatting to string.
        See Also:
        Constant Field Values
      • SORT_OBJECT

        public static final int SORT_OBJECT
        To be used in the qsort method; the Object array will be compared converting to string.
        See Also:
        Constant Field Values
      • SORT_STRING

        public static final int SORT_STRING
        To be used in the qsort method; the Object array contain String objects (case sensitive).
        See Also:
        Constant Field Values
      • SORT_INT

        public static final int SORT_INT
        To be used in the qsort method; the Object array contain String objects that represents an integer.
        See Also:
        Constant Field Values
      • SORT_DOUBLE

        public static final int SORT_DOUBLE
        To be used in the qsort method; the Object array contain String objects that represents a double.
        See Also:
        Constant Field Values
      • SORT_DATE

        public static final int SORT_DATE
        To be used in the qsort method; the Object array contain String objects that represents a Date with day, month and year.
        See Also:
        Constant Field Values
      • SORT_COMPARABLE

        public static final int SORT_COMPARABLE
        To be used in the qsort method; the Object array contain Comparable objects.
        See Also:
        Constant Field Values
      • SORT_STRING_NOCASE

        public static final int SORT_STRING_NOCASE
        To be used in the qsort method; the Object array contain String objects, using a case insensitive (and slower) algorithm. This mode is never set unless you specify it.
        See Also:
        Constant Field Values
      • DOUBLE_POSITIVE_INFINITY_BITS

        public static final long DOUBLE_POSITIVE_INFINITY_BITS
        See Also:
        Constant Field Values
      • DOUBLE_NEGATIVE_INFINITY_BITS

        public static final long DOUBLE_NEGATIVE_INFINITY_BITS
        See Also:
        Constant Field Values
      • DOUBLE_POSITIVE_INFINITY_VALUE

        public static final double DOUBLE_POSITIVE_INFINITY_VALUE
      • DOUBLE_NEGATIVE_INFINITY_VALUE

        public static final double DOUBLE_NEGATIVE_INFINITY_VALUE
      • DOUBLE_NAN_VALUE

        public static final double DOUBLE_NAN_VALUE
    • Method Detail

      • registerCharacterConverter

        public static void registerCharacterConverter​(AbstractCharacterConverter characterConverter)
        Registers the given subclass of AbstractCharacterConverter to the list of encodings supported to this instance of the running TotalCross VM
        Parameters:
        characterConverter - the AbstractCharacterConverter to be registered to this instance of the running TotalCross VM
      • setDefaultConverter

        public static boolean setDefaultConverter​(java.lang.String name)
        Changes the default Character Converter to the given one. Use like
        Convert.setDefaultConverter("UTF8");
        to change all bytes_to_char and char_to_bytes operations to use UTF8 instead.
        Convert.setDefaultConverter("");
        sets back the default encoder.
        Returns:
        true if the given encoder was found, false otherwise. If not found, the encoder is reset to the default one (ISO 8859-1).
        Since:
        SuperWaba 4.1
        See Also:
        charConverter, CharacterConverter, UTF8CharacterConverter
      • charsetForName

        public static java.nio.charset.Charset charsetForName​(java.lang.String charsetName)
                                                       throws java.lang.IllegalArgumentException
        Returns a charset object for the named charset.
        Parameters:
        charsetName - The name of the requested charset; may be either a canonical name or an alias
        Returns:
        A charset object for the named charset or null if no support for the named charset is available
        Throws:
        java.lang.IllegalArgumentException - If the given charsetName is null
      • toTitleCase

        public static char toTitleCase​(char ch)
        Converts a Unicode character into its titlecase equivalent mapping. If a mapping does not exist, then the character passed is returned. Note that isTitleCase(toTitleCase(ch)) does not always return true.
        Parameters:
        ch - character to convert to titlecase
        Returns:
        titlecase mapping of ch, or ch if titlecase mapping does not exist
        See Also:
        toLowerCase(char), toUpperCase(char)
      • cloneStringArray

        public static java.lang.String[] cloneStringArray​(java.lang.String[] strs)
        Creates a copy of the given array.
      • toStringArray

        public static java.lang.String[] toStringArray​(java.lang.Object[] objs)
        Converts the given object array to a String array by calling toString in each object.
      • toShort

        public static short toShort​(java.lang.String s)
                             throws InvalidNumberException
        Convert a string to the short type. Note that this method is slower than (short)Convert.toInt()
        Throws:
        InvalidNumberException
        Since:
        TotalCross 1.22
      • toInt

        public static int toInt​(java.lang.String s)
                         throws InvalidNumberException
        Converts the given String to an int. The number may be prefixed with 0's.
        Throws:
        InvalidNumberException - If the string passed is not a valid number
      • toInt

        public static int toInt​(java.lang.String s,
                                int defaultValue)
        Converts the given String to an int. The number may be prefixed with 0's. If the string is not a valid number, returns defaultValue.
        Since:
        TotalCross 2.0
      • toDouble

        public static double toDouble​(java.lang.String s,
                                      double defaultValue)
        Converts the given String to a double. If the string is not a valid number, returns defaultValue.
        Since:
        TotalCross 2.0
      • toString

        public static java.lang.String toString​(boolean b)
        Converts the given boolean to a String.
      • toString

        public static java.lang.String toString​(char c)
        Converts the given char to a String.
      • doubleToIntBits

        public static int doubleToIntBits​(double f)
        Converts the given double to its bit representation in IEEE 754 format, using 4 bytes instead of 8 (a convertion to float is applied).
      • intBitsToDouble

        public static double intBitsToDouble​(int i)
        Converts the given IEEE 754 bit representation of a float to a double.
      • toString

        public static java.lang.String toString​(int i)
        Converts the given int to a String.
      • toString

        public static java.lang.String toString​(double val,
                                                int decimalCount)
        Converts the given double to a String, formatting it with the given number of decimal places.
        Since:
        SuperWaba 2.0
      • toDouble

        public static double toDouble​(java.lang.String s)
                               throws InvalidNumberException
        Converts the String to a double.
        Throws:
        InvalidNumberException - If the string passed is not a valid number
        Since:
        SuperWaba 2.0
      • toString

        public static java.lang.String toString​(java.lang.String doubleValue,
                                                int n)
                                         throws InvalidNumberException
        formats a String as a double, rounding with n decimal places.
        Throws:
        InvalidNumberException - If the string passed is not a valid number
      • doubleToLongBits

        public static long doubleToLongBits​(double value)
        Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.

        Bit 63 represents the sign of the floating-point number. Bits 62-52 represent the exponent. Bits 51-0 represent the significand (sometimes called the mantissa) of the floating-point number.

        If the argument is positive infinity, the result is 0x7ff0000000000000L.

        If the argument is negative infinity, the result is 0xfff0000000000000L.

        If the argument is NaN, the result is 0x7ff8000000000000L.

        Parameters:
        value - a double precision floating-point number.
        Returns:
        the bits that represent the floating-point number.
        Since:
        SuperWaba 2.0
      • longBitsToDouble

        public static double longBitsToDouble​(long bits)
        Returns the double-float corresponding to a given bit represention. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "double precision" bit layout. That floating-point value is returned as the result.

        If the argument is 0x7f80000000000000L, the result is positive infinity.

        If the argument is 0xff80000000000000L, the result is negative infinity.

        If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is NaN. All IEEE 754 NaN values are, in effect, lumped together by the Java language into a single value.

        Parameters:
        bits - any long integer.
        Returns:
        the double floating-point value with the same bit pattern.
        Since:
        SuperWaba 2.0
      • digitOf

        public static int digitOf​(char ch,
                                  int radix)
        returns the value of the digit in the specified radix. this method is simplified to only handle the standard ascii table.
        Since:
        SuperWaba 2.0
      • forDigit

        public static char forDigit​(int digit,
                                    int radix)
        returns the digit in the corresponding radix.
        Since:
        SuperWaba 2.0
      • toString

        public static java.lang.String toString​(long l)
        Converts the long to a String at base 10.
        Since:
        SuperWaba 2.0
      • toString

        public static java.lang.String toString​(int i,
                                                int radix)
        Converts the int to a String in the given radix. Radix can be 2, 8, 10 or 16.
      • toString

        public static java.lang.String toString​(long i,
                                                int radix)
        Converts the long to a String in the given radix. Radix can be 2, 8, 10 or 16.
      • toString4D

        public static java.lang.String toString4D​(long i,
                                                  int radix)
      • toLong

        public static long toLong​(java.lang.String s,
                                  int radix)
                           throws InvalidNumberException
        Converts the String to a long in the given radix. Radix can range from 2 to 16. At any error, 0 is returned.
        Throws:
        InvalidNumberException - If the string passed is not a valid number
        Since:
        SuperWaba 2.0
      • toString

        public static java.lang.String toString​(double d)
        Converts the given double to a String, formatting it using scientific notation.
        Since:
        SuperWaba 2.0
      • toLowerCase

        public static char toLowerCase​(char c)
        Converts the char to lower case letter
      • toUpperCase

        public static char toUpperCase​(char c)
        Converts the char to upper case letter
      • numberPad

        public static java.lang.String numberPad​(java.lang.String s,
                                                 int size)
        Pads the given string with the char 160 (that have the same width of a number), putting it before or after the string.
        Since:
        TotalCross 1.53
      • numberPad

        public static java.lang.String numberPad​(int s,
                                                 int size)
        Converts the int to String and pads it with the char 160 (that have the same width of a number) at left.
        Since:
        TotalCross 1.53
        See Also:
        numberPad(String, int)
      • zeroPad

        public static java.lang.String zeroPad​(java.lang.String s,
                                               int size)
        Pads the string with zeroes at left.
      • zeroPad

        public static java.lang.String zeroPad​(int s,
                                               int size)
        Converts the int to String and pads it with zeroes at left.
        Since:
        TotalCross 1.15
        See Also:
        zeroPad(String, int)
      • tokenizeString

        public static java.lang.String[] tokenizeString​(java.lang.String input,
                                                        char delim)
        Tokenize the given input string. If there's no delim chars in input, returns the input string. Two consecutive delimeters results in an empty String, not in NULL.
      • tokenizeString

        public static java.lang.String[] tokenizeString​(java.lang.String input,
                                                        java.lang.String delim)
        Tokenize the given input string. If there's no delim chars in input, returns the input string. The delim parameter is not a set of possible single characters: it is a whole string that is searched inside the given input.

        Note that this method is much slower than the one with same name, which receives a char instead of a String as the delimiter.
        Since:
        SuperWaba 4.02
      • tokenizeString

        public static java.lang.String[] tokenizeString​(java.lang.String input,
                                                        char... delims)
        Splits the specified string around matches of the given delimiters. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
        Parameters:
        input - a string to be parsed.
        delims - the delimiters.
        Returns:
        the array of strings computed by splitting this string around matches of the given delimiters
        Since:
        TotalCross 1.15
      • insertLineBreak

        public static java.lang.String insertLineBreak​(int maxWidth,
                                                       FontMetrics fm,
                                                       java.lang.String text)
        This method is useful to insert line breaks into the text used in the MessageBox constructor. It receives a String and returns the parsed String. Here is an example of use: Convert.insertLineBreak(Settings.screenWidth-10,getFontMetrics(getFont()),"This is a very long text and i dont want to waste my time parsing it to be fit in the MessageBox!");

        If you want to use another separator besides \n, just call replace('\n',separator) in the returned string.
        Parameters:
        maxWidth - the maximum width that is permitted for each line. For a text used in the MessageBox class, use Settings.screenWidth-6 or a lower number.
        fm - the FontMetrics of the font you will use to display the text
        text - the text to be parsed
        Returns:
        the parsed text
      • insertLineBreakBalanced

        public static java.lang.String insertLineBreakBalanced​(int maxWidth,
                                                               FontMetrics fm,
                                                               java.lang.String text)
        This method is useful to insert line breaks into the text used in the Toast.show; it behaves like insertLineBreak but tries to split the string in a balanced number of chars.
        See Also:
        insertLineBreak(int, FontMetrics, String)
      • getBreakPos

        public static int getBreakPos​(FontMetrics fm,
                                      java.lang.StringBuffer sb,
                                      int start,
                                      int width,
                                      boolean doWordWrap)
        Finds the best position to break the line, with word-wrap and respecting \n.
        Since:
        TotalCross 1.0
      • qsort

        public static void qsort​(java.lang.Object[] items,
                                 int first,
                                 int last)
        Applies the Quick Sort algorithm to the elements of the given array. The type of sort is automatically detected, You may call this way: Convert.qsort(items,0,items.length-1). You can sort subportions of the array as well.
        See Also:
        SORT_AUTODETECT
      • detectSortType

        public static int detectSortType​(java.lang.Object item)
        Returns the sort type for the given item sample (which is usually the first item of the array being sorted) based on:
        • If item is not of String type, SORT_OBJECT is returned. The toString method will be called for each Object to convert from Object to something comparable.
        • If item starts with "0.0" or convert to double works, returns SORT_DOUBLE.
        • If item equals "0" or converted to int works, returns SORT_INT.
        • If item converted to totalcross.util.Date equals the column's data itself, returns SORT_DATE.
        • else, the sort type is SORT_STRING.
        Important: if your data are floating point ones, be sure that the first element is NOT something that can be converted to an integer. For example, be sure it is "2.0" and not "2" (converting "2.0" to int results in 0, because the dot is not part of an integer number.
        See Also:
        SORT_AUTODETECT, SORT_OBJECT, SORT_STRING, SORT_INT, SORT_DOUBLE, SORT_DATE, SORT_COMPARABLE, SORT_STRING_NOCASE
      • qsort

        public static void qsort​(java.lang.Object[] items,
                                 int first,
                                 int last,
                                 int sortType)
        Applies the Quick Sort algorithm to the elements of the given array, sorting in ascending order. If they are Strings, the sort will be much faster because a direct cast to String is done; otherwise, if they are not strings, the toString() method is used to return the string that will be used for comparision. You may call this way:
         Convert.qsort(items,0,items.length-1, SORT_AUTODETECT).
         
        You can sort subportions of the array as well.
        See Also:
        SORT_AUTODETECT, SORT_OBJECT, SORT_STRING, SORT_INT, SORT_DOUBLE, SORT_DATE, SORT_COMPARABLE, SORT_STRING_NOCASE, detectSortType(Object)
      • qsort

        public static void qsort​(java.lang.Object[] items,
                                 int first,
                                 int last,
                                 int sortType,
                                 boolean ascending)
        Applies the Quick Sort algorithm to the elements of the given array, in ascending or descending order. If they are Strings, the sort will be much faster because a direct cast to String is done; otherwise, if they are not strings, the toString() method is used to return the string that will be used for comparision. You may call this way:
         Convert.qsort(items,0,items.length-1, Convert.SORT_DATE, true).
         
        You can sort subportions of the array as well.
        Use the SORT_xxx to define the sort type, or SORT_AUTODETECT to automatically detect it.
        See Also:
        SORT_AUTODETECT, SORT_OBJECT, SORT_STRING, SORT_INT, SORT_DOUBLE, SORT_DATE, SORT_COMPARABLE, SORT_STRING_NOCASE, detectSortType(Object)
      • unsigned2hex

        public static java.lang.String unsigned2hex​(int b,
                                                    int places)
        Returns a String with the conversion of the unsigned integer to hexadecimal, using the given number of places (up to 8). This routine is 90 times faster than toString(long, int). The hex digits are in upper case. If places is smaller than the total number of digits, then the number will be truncated, and the lower part of the number will be returned.
        Since:
        SuperWaba 3.0
      • hex2unsigned

        public static int hex2unsigned​(java.lang.String s)
        Returns an int with the conversion of the unsigned hexadecimal to integer. The hex digits can be in upper case or lower case.
        Since:
        TotalCross 1.0
      • hex2signed

        public static int hex2signed​(java.lang.String s)
        Returns an int with the conversion of the signed hexadecimal to integer. The hex digits can be in upper case or lower case.
        Parameters:
        s - the string representation of the signed hexadecimal
        Returns:
        a signed integer with the converted value
        Since:
        TotalCross 1.15
      • bytesToHexString

        public static java.lang.String bytesToHexString​(byte[] b)
        Converts the given sequence of bytes to a String. The hexadecimal String is in upper case.
        Since:
        TotalCross 1.0 beta 4
      • bytesToHexString

        public static java.lang.String bytesToHexString​(byte[] b,
                                                        int off,
                                                        int len)
        Converts the given sequence of bytes to a String. The hexadecimal String is in upper case.
        Since:
        TotalCross 1.01
      • hexStringToBytes

        public static byte[] hexStringToBytes​(java.lang.String s)
        Converts the given String into an array of bytes. Each two consecutive characters are converted into a byte.
        Parameters:
        s - The hex string to convert to bytes
        Since:
        TotalCross 1.0 beta 4
      • hexStringToBytes

        public static byte[] hexStringToBytes​(java.lang.String s,
                                              boolean ignoreNonHexChars)
        Converts the given String into an array of bytes. Each two consecutive characters are converted into a byte.
        Parameters:
        s - The hex string to convert to bytes
        ignoreNonHexChars - Flag indicating if non hex chars should be ignored or not
        Since:
        TotalCross 1.0 beta 5
      • rol

        public static long rol​(long i,
                               int n,
                               int bits)
        Do a rol of n bits in the given i(nt). n must be < bits. This differs from the shift left operator (<<) in that the bits are not lost, they are reinserted in order at the right.
        Parameters:
        i - The positive number to be rolled
        n - The number of bits to be rolled, where 0 <= n <= bits
        bits - The number of bits that will be considered, where 0 < bits <= 64. Bits outside the bit range are masked out. If 0 is given, 64 will be used. In order to make it faster, the routine does not check for out-of-bounds parameters.
      • ror

        public static long ror​(long i,
                               int n,
                               int bits)
        Do a ror of n bits in the given i(nt). n must be < bits. This differs from the shift right operator (>>) in that the bits are not lost, they are reinserted in order at the left.
        Parameters:
        i - The positive number to be rolled
        n - The number of bits to be rolled, where 0 <= n <= bits
        bits - The number of bits that will be considered, where 0 < bits <= 64 Bits outside the bit range are masked out. If 0 is given, 64 will be used. In order to make it faster, the routine does not check for out-of-bounds parameters.
      • chars2int

        public static int chars2int​(java.lang.String fourChars)
        Convert a creator id or a type to an int.
        Parameters:
        fourChars - Four characters representing a creator id.
      • int2chars

        public static java.lang.String int2chars​(int i)
        Convert an int to a creator id or a type.
      • dup

        public static java.lang.String dup​(char c,
                                           int count)
        Duplicates the given char count times.
        Throws:
        java.lang.IllegalArgumentException - If count is below 0
        Since:
        SuperWaba 5.72
      • hashCode

        public static int hashCode​(java.lang.StringBuffer sb)
        Gets the hashcode of the string being stored by this StringBuffer. This saves memory since there's no need to convert the StringBuffer to a String.
        Since:
        TotalCross 1.0
      • remove

        public static java.lang.String remove​(java.lang.String source,
                                              java.lang.String chars)
        Removes the given set of chars from the String. Example:
         String s = Convert.remove("abcdef","df"); // returns "abce";
         
        Since:
        TotalCross 3.05
      • replace

        public static java.lang.String replace​(java.lang.String source,
                                               java.lang.String from,
                                               java.lang.String to)
        Replace all occurences of the from String by the to String in the given source String.
        Since:
        TotalCross 1.0
      • numberOf

        public static int numberOf​(java.lang.String s,
                                   char c)
        Returns the number of chars in a String.
        Since:
        TotalCross 1.0
      • insertAt

        public static void insertAt​(java.lang.StringBuffer sb,
                                    int pos,
                                    char c)
        Inserts the given char at the position in the StringBuffer. This is to keep compatibility with JDK 1.1.x in browsers, because StringBuffer.insert was introduced in JDK 1.2 only.
        Since:
        TotalCross 1.0
      • append

        public static void append​(java.lang.StringBuffer sb,
                                  char c,
                                  int count)
        This method appends a number of characters into a StringBuffer. It greatly reduces the amount of memory needed. For example, if you're building a fixed-width line with spaces, you can do:
         int dif = colWidth - value.length();
         sb.apppend(value);
         Convert.append(sb, ' ', dif); // pad column with spaces
         
        ... where sb is the StringBuffer you're using to concatenate the strings, value is the value you're appending, and colWidth is the current column width.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - If count is below 0.
        Since:
        TotalCross 1.2
      • getBytes

        public static byte[] getBytes​(java.lang.StringBuffer sb)
        Returns the bytes of the given StringBuffer using the current CharacterConverter instance. This can help save memory since it does not create the intermediate String, in the usual code:
         byte[] b = sb.toString().getBytes();
         
        There's no performance gain in Blackberry nor in Java SE.
        Since:
        TotalCross 1.23
      • zeroUnpad

        public static java.lang.String zeroUnpad​(java.lang.String s)
        Strips the left zeros of the String. Note that Convert.toInt DOES support zeros before the number, so there's no need to call this method before converting "0011" into 11.
        Since:
        TotalCross 1.0
      • max

        public static int max​(int... ai)
        Returns the maximum value among the given ints
        Since:
        TotalCross 1.15
      • sum

        public static int sum​(int[] ai,
                              int startIndex,
                              int endIndex)
        Returns the sum of the given ints, in the given range (startIndex <= x < endIndex).
        Since:
        TotalCross 2.22
      • sum

        public static int sum​(int... ai)
        Returns the sum of the given ints
        Since:
        TotalCross 1.15
      • min

        public static int min​(int... ai)
        Returns the minimum value among the given ints
        Since:
        TotalCross 1.15
      • spacePad

        public static java.lang.String spacePad​(java.lang.String what,
                                                int count,
                                                boolean before)
        Pads the given string with spaces, putting it before or after the string. If count is less than the string's length, the string is returned without being changed.
        Since:
        TotalCross 1.15
      • appendPath

        public static java.lang.String appendPath​(java.lang.String path1,
                                                  java.lang.String path2)
        Concatenates two strings ensuring there's a single slash between them. Strings are normalized using / as path separator and removing any duplicate separators. You must ensure that both strings are not null neither empty.
        Since:
        TotalCross 1.0
      • normalizePath

        public static java.lang.String normalizePath​(java.lang.String path)
        Normalizes a string using / as the path separator and removing any duplicate separators.
        Parameters:
        path - The string to normalize.
        Returns:
        The normalized string.
        Since:
        TotalCross 1.20
      • ints2bytes

        public static byte[] ints2bytes​(int[] from,
                                        int count)
        Converts the given int array into a byte array. The int array must be in big endian format (least significant bits at last). This helps decrease file size if you're storing in the code an array of bytes. If the byte array has 13 or more elements, using this technique will decrease the file's size.

        For example, the array {(byte)0x12,(byte)0x34,(byte)0x56,(byte)0x78,(byte)0xAB,(byte)0xCD,(byte)0xEF} must be stored as {0x78563412,0x00EFCDAB} and you must call this method passing 7 for count.

        You can use the following method to dump to the console the convertion of your byte arrays:
         static void bytes2intsDump(byte[] in)
         {
            System.out.print("Convert.ints2bytes(new int[]{");
            for (int i = 0; i < in.length;)
            {
               int b1 = i < in.length ? in[i++] : 0;
               int b2 = i < in.length ? in[i++] : 0;
               int b3 = i < in.length ? in[i++] : 0;
               int b4 = i < in.length ? in[i++] : 0;
               int out = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
               System.out.print("0x"+Convert.unsigned2hex(out,8)+",");
            }
            System.out.println("}, "+in.length+");");
         }
         
        For example, calling it with:
         byte[] in = {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7};
         bytes2intsDump(in);
         
        Will output this:
         Convert.ints2bytes(new int[]{0x04030201,0x08070605,0x04030201,0x00070605}, 15);
         
        Note that you must not include the bytes2intsDump into the program that is going to the device. It is a desktop-only utility method.
        Parameters:
        from - The source int array
        count - The length of the target byte array. It may differ from from.length*4 because the last value of the array may be padded.
        Since:
        TotalCross 1.01
      • fill

        public static void fill​(char[] a,
                                int from,
                                int to,
                                char value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to.
      • fill

        public static void fill​(boolean[] a,
                                int from,
                                int to,
                                boolean value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to.
      • fill

        public static void fill​(int[] a,
                                int from,
                                int to,
                                int value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to.
      • fill

        public static void fill​(double[] a,
                                int from,
                                int to,
                                double value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to.
      • fill

        public static void fill​(short[] a,
                                int from,
                                int to,
                                int value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to. The int value is casted to short.
      • fill

        public static void fill​(byte[] a,
                                int from,
                                int to,
                                int value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to. The int value is casted to byte.
      • fill

        public static void fill​(long[] a,
                                int from,
                                int to,
                                long value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to.
      • fill

        public static void fill​(java.lang.Object[] a,
                                int from,
                                int to,
                                java.lang.Object value)
        Fills the given array, within the range, with the value specified. The array is filled as from <= n < to.
      • toCurrencyString

        public static java.lang.String toCurrencyString​(double d,
                                                        int decimalPlaces)
        Returns the given double formatted using the Settings decimal and thousands separators.
        Parameters:
        d - the double to be converted to String.
        decimalPlaces - the number of decimal places. Must be >= -1.
        Since:
        TotalCross 1.01
        See Also:
        toCurrencyString(String,int)
      • toCurrencyString

        public static java.lang.String toCurrencyString​(java.lang.String s,
                                                        int decimalPlaces)
        Returns the given double formatted using the Settings decimal and thousands separators.
        Parameters:
        s - the double String to be converted to String. It must already have the given number of decimal places.
        decimalPlaces - the number of decimal places. Must be >= -1.
        Since:
        TotalCross 1.14
        See Also:
        toCurrencyString(double,int)
      • tokenizeArguments

        public static java.lang.String[] tokenizeArguments​(java.lang.String arg)
        Tokenizes a command line arguments, breaking it into an array of strings. Useful when getting parameters from MainWindow.getCommandLine as an unique string and changing it to a String array as in Java's main method. Supports parsing strings with quotes, but the quote itself is not returned. Consecutive spaces outside "" are ignored.
        Since:
        TotalCross 1.2
      • isUpperCase

        public static boolean isUpperCase​(char c)
        Returns true if the given char is in uppercase. Supports unicode.
        Since:
        TotalCross 1.13
      • isLowerCase

        public static boolean isLowerCase​(char c)
        Returns true if the given char is in lowercase. Supports unicode.
        Since:
        TotalCross 1.13
      • getDistancePoint2Rect

        public static double getDistancePoint2Rect​(int x,
                                                   int y,
                                                   int x1,
                                                   int y1,
                                                   int x2,
                                                   int y2)
        Computes the distance between a point (x,y) and a rectangle (corners x1,y1 and x2,y2). Note that, if the point is inside the rect, 0 is returned.
        Since:
        TotalCross 1.2
      • appendTimeStamp

        public static java.lang.StringBuffer appendTimeStamp​(java.lang.StringBuffer sb,
                                                             Time t,
                                                             boolean appendDate,
                                                             boolean appendTime)
        Appends the given timestamp to a StringBuffer, using the current Date format and separators.
        Since:
        TotalCross 1.24
      • getFilePath

        public static java.lang.String getFilePath​(java.lang.String fullPath)
        Given a full path name, returns the normalized path of the file, without the slash. If the file contains only a name, without slashes, returns null.
        Since:
        TotalCross 1.27
      • getFileName

        public static java.lang.String getFileName​(java.lang.String fullPath)
        Given a full path name, returns the name of the file. If the file contains only a name, without slashes, returns the fullPath.
        Since:
        TotalCross 1.27
      • getFileNameOnly

        public static java.lang.String getFileNameOnly​(java.lang.String fullPath)
        Given a full path name or a single file name, returns the name of the file without the extension.
        Since:
        TotalCross 1.27
      • removeAccentuation

        public static java.lang.String removeAccentuation​(java.lang.String s)
        Returns the given string without accentuation characters, using the unicode range 0-255
      • equals

        public static boolean equals​(byte[] b1,
                                     byte[] b2)