Class Convert
- java.lang.Object
-
- totalcross.sys.Convert
-
public final class Convert extends java.lang.ObjectConvert is used to convert between objects and basic types. It also contains many other utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static char[]b2hstatic AbstractCharacterConvertercharConverterThe bytes are converted to char and vice-versa using the CharacterConverter associated in this charConverter member.static java.lang.StringCRLFstatic byte[]CRLF_BYTESstatic longDOUBLE_NAN_BITSstatic doubleDOUBLE_NAN_VALUEstatic longDOUBLE_NEGATIVE_INFINITY_BITSstatic doubleDOUBLE_NEGATIVE_INFINITY_VALUEstatic longDOUBLE_POSITIVE_INFINITY_BITSstatic doubleDOUBLE_POSITIVE_INFINITY_VALUEstatic charMAX_CHAR_VALUEThe maximum char value:static intMAX_DOUBLE_DIGITSThe maximum number of digits in a double value, used when formatting to string.static doubleMAX_DOUBLE_VALUEThe maximum double value: 1.7976931348623157E308dstatic intMAX_INT_VALUEThe maximum int value: 2147483647static longMAX_LONG_VALUEThe maximum long value: 9223372036854775807static shortMAX_SHORT_VALUEThe maximum short value: 32767static charMIN_CHAR_VALUEThe minimum char value: ' 'static doubleMIN_DOUBLE_VALUEThe minimum double value: 4.9E-324dstatic intMIN_INT_VALUEThe minimum int value: -2147483648static longMIN_LONG_VALUEThe minimum long value: -9223372036854775808static shortMIN_SHORT_VALUEThe minimum short value: -32768static intSORT_AUTODETECTTo be used in the qsort method; the type of sort will be automatically detected.static intSORT_COMPARABLETo be used in the qsort method; the Object array contain Comparable objects.static intSORT_DATETo be used in the qsort method; the Object array contain String objects that represents a Date with day, month and year.static intSORT_DOUBLETo be used in the qsort method; the Object array contain String objects that represents a double.static intSORT_INTTo be used in the qsort method; the Object array contain String objects that represents an integer.static intSORT_OBJECTTo be used in the qsort method; the Object array will be compared converting to string.static intSORT_STRINGTo be used in the qsort method; the Object array contain String objects (case sensitive).static intSORT_STRING_NOCASETo be used in the qsort method; the Object array contain String objects, using a case insensitive (and slower) algorithm.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappend(java.lang.StringBuffer sb, char c, int count)This method appends a number of characters into a StringBuffer.static java.lang.StringappendPath(java.lang.String path1, java.lang.String path2)Concatenates two strings ensuring there's a single slash between them.static java.lang.StringBufferappendTimeStamp(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.StringbytesToHexString(byte[] b)Converts the given sequence of bytes to a String.static java.lang.StringbytesToHexString(byte[] b, int off, int len)Converts the given sequence of bytes to a String.static intchars2int(java.lang.String fourChars)Convert a creator id or a type to an int.static java.nio.charset.CharsetcharsetForName(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 intdetectSortType(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 intdigitOf(char ch, int radix)returns the value of the digit in the specified radix.static intdoubleToIntBits(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 longdoubleToLongBits(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.Stringdup(char c, int count)Duplicates the given charcounttimes.static booleanequals(byte[] b1, byte[] b2)static voidfill(boolean[] a, int from, int to, boolean value)Fills the given array, within the range, with the value specified.static voidfill(byte[] a, int from, int to, int value)Fills the given array, within the range, with the value specified.static voidfill(char[] a, int from, int to, char value)Fills the given array, within the range, with the value specified.static voidfill(double[] a, int from, int to, double value)Fills the given array, within the range, with the value specified.static voidfill(int[] a, int from, int to, int value)Fills the given array, within the range, with the value specified.static voidfill(long[] a, int from, int to, long value)Fills the given array, within the range, with the value specified.static voidfill(short[] a, int from, int to, int value)Fills the given array, within the range, with the value specified.static voidfill(java.lang.Object[] a, int from, int to, java.lang.Object value)Fills the given array, within the range, with the value specified.static charforDigit(int digit, int radix)returns the digit in the corresponding radix.static intgetBreakPos(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 doublegetDistancePoint2Rect(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.StringgetFileName(java.lang.String fullPath)Given a full path name, returns the name of the file.static java.lang.StringgetFileNameOnly(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.StringgetFilePath(java.lang.String fullPath)Given a full path name, returns the normalized path of the file, without the slash.static inthashCode(java.lang.StringBuffer sb)Gets the hashcode of the string being stored by this StringBuffer.static inthex2signed(java.lang.String s)Returns an int with the conversion of the signed hexadecimal to integer.static inthex2unsigned(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 voidinsertAt(java.lang.StringBuffer sb, int pos, char c)Inserts the given char at the position in the StringBuffer.static java.lang.StringinsertLineBreak(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.StringinsertLineBreakBalanced(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.Stringint2chars(int i)Convert an int to a creator id or a type.static doubleintBitsToDouble(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 booleanisLowerCase(char c)Returns true if the given char is in lowercase.static booleanisUpperCase(char c)Returns true if the given char is in uppercase.static doublelongBitsToDouble(long bits)Returns the double-float corresponding to a given bit represention.static intmax(int... ai)Returns the maximum value among the given intsstatic intmin(int... ai)Returns the minimum value among the given intsstatic java.lang.StringnormalizePath(java.lang.String path)Normalizes a string using / as the path separator and removing any duplicate separators.static intnumberOf(java.lang.String s, char c)Returns the number of chars in a String.static java.lang.StringnumberPad(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.StringnumberPad(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 voidqsort(java.lang.Object[] items, int first, int last)Applies the Quick Sort algorithm to the elements of the given array.static voidqsort(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 voidqsort(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 voidregisterCharacterConverter(AbstractCharacterConverter characterConverter)Registers the given subclass of AbstractCharacterConverter to the list of encodings supported to this instance of the running TotalCross VMstatic java.lang.Stringremove(java.lang.String source, java.lang.String chars)Removes the given set of chars from the String.static java.lang.StringremoveAccentuation(java.lang.String s)Returns the given string without accentuation characters, using the unicode range 0-255static java.lang.Stringreplace(java.lang.String source, java.lang.String from, java.lang.String to)Replace all occurences of thefromString by thetoString in the givensourceString.static longrol(long i, int n, int bits)Do a rol of n bits in the given i(nt).static longror(long i, int n, int bits)Do a ror of n bits in the given i(nt).static booleansetDefaultConverter(java.lang.String name)Changes the default Character Converter to the given one.static java.lang.StringspacePad(java.lang.String what, int count, boolean before)Pads the given string with spaces, putting it before or after the string.static intsum(int... ai)Returns the sum of the given intsstatic intsum(int[] ai, int startIndex, int endIndex)Returns the sum of the given ints, in the given range (startIndex <= x < endIndex).static java.lang.StringtoCurrencyString(double d, int decimalPlaces)Returns the given double formatted using the Settings decimal and thousands separators.static java.lang.StringtoCurrencyString(java.lang.String s, int decimalPlaces)Returns the given double formatted using the Settings decimal and thousands separators.static doubletoDouble(java.lang.String s)Converts the String to a double.static doubletoDouble(java.lang.String s, double defaultValue)Converts the given String to a double.static inttoInt(java.lang.String s)Converts the given String to an int.static inttoInt(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 longtoLong(java.lang.String s)Converts the String to a long.static longtoLong(java.lang.String s, int radix)Converts the String to a long in the given radix.static chartoLowerCase(char c)Converts the char to lower case letterstatic shorttoShort(java.lang.String s)Convert a string to the short type.static java.lang.StringtoString(boolean b)Converts the given boolean to a String.static java.lang.StringtoString(char c)Converts the given char to a String.static java.lang.StringtoString(double d)Converts the given double to a String, formatting it using scientific notation.static java.lang.StringtoString(double val, int decimalCount)Converts the given double to a String, formatting it with the given number of decimal places.static java.lang.StringtoString(int i)Converts the given int to a String.static java.lang.StringtoString(int i, int radix)Converts the int to a String in the given radix.static java.lang.StringtoString(long l)Converts the long to a String at base 10.static java.lang.StringtoString(long i, int radix)Converts the long to a String in the given radix.static java.lang.StringtoString(java.lang.String doubleValue, int n)formats a String as a double, rounding with n decimal places.static java.lang.StringtoString4D(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 chartoTitleCase(char ch)Converts a Unicode character into its titlecase equivalent mapping.static chartoUpperCase(char c)Converts the char to upper case letterstatic java.lang.Stringunsigned2hex(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.StringzeroPad(int s, int size)Converts the int to String and pads it with zeroes at left.static java.lang.StringzeroPad(java.lang.String s, int size)Pads the string with zeroes at left.static java.lang.StringzeroUnpad(java.lang.String s)Strips the left zeros of the String.
-
-
-
Field Detail
-
CRLF
public static final java.lang.String CRLF
- See Also:
- Constant Field Values
-
CRLF_BYTES
public static final byte[] CRLF_BYTES
-
b2h
public static char[] b2h
-
charConverter
public static AbstractCharacterConverter charConverter
The bytes are converted to char and vice-versa using the CharacterConverter associated in this charConverter member.
-
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_AUTODETECT
public static final int SORT_AUTODETECT
To be used in the qsort method; the type of sort will be automatically detected.- See Also:
detectSortType(Object), 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_NAN_BITS
public static final long DOUBLE_NAN_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 likeConvert.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.IllegalArgumentExceptionReturns 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 InvalidNumberExceptionConvert 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 InvalidNumberExceptionConverts 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 InvalidNumberExceptionConverts 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 InvalidNumberExceptionformats 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
0x7ff0000000000001Lthrough0x7fffffffffffffffLor in the range0xfff0000000000001Lthrough0xffffffffffffffffL, the result is NaN. All IEEE 754 NaN values are, in effect, lumped together by the Java language into a single value.- Parameters:
bits- anylonginteger.- Returns:
- the
doublefloating-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) throws InvalidNumberExceptionConverts the String to a long.- Throws:
InvalidNumberException- If the string passed is not a valid number- Since:
- SuperWaba 2.0
-
toLong
public static long toLong(java.lang.String s, int radix) throws InvalidNumberExceptionConverts 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, useSettings.screenWidth-6or a lower number.fm- the FontMetrics of the font you will use to display the texttext- 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.
-
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.
-
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.
-
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.
-
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 bytesignoreNonHexChars- 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 rolledn- The number of bits to be rolled, where 0 <= n <= bitsbits- 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 rolledn- The number of bits to be rolled, where 0 <= n <= bitsbits- 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 charcounttimes.- 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 thefromString by thetoString in the givensourceString.- 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 arraycount- The length of the target byte array. It may differ fromfrom.length*4because 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)
-
-