📄 convert.java
字号:
return toHexString(data, offset, length, ""); } /** * Converts a byte array into a hex-encoded String, using the provided * delimeter. * * @param data The byte[] to convert to a hex-encoded string * @param delimeter the delimeter to place between each byte of data * @return Hex-encoded String */ public static final String toHexString(byte[] data, String delimeter) { return toHexString(data, 0, data.length, delimeter); } /** * Converts a byte array into a hex-encoded String, using the provided * delimeter. * * @param data The byte[] to convert to a hex-encoded string * @param offset the offset to start converting bytes * @param length the number of bytes to convert * @param delimeter the delimeter to place between each byte of data * @return Hex-encoded String */ public static final String toHexString(byte[] data, int offset, int length, String delimeter) { StringBuffer value = new StringBuffer(length*(2+delimeter.length())); int max = length+offset; int lastDelim = max-1; for(int i=offset; i<max; i++) { byte bits = data[i]; value.append(lookup_hex[(bits>>4)&0x0F]); value.append(lookup_hex[bits&0x0F]); if(i<lastDelim) value.append(delimeter); } return value.toString(); } /** * <P>Converts a single byte into a hex-encoded string.</P> * * @param bValue the byte to encode * @return String Hex-encoded String */ public static final String toHexString(byte bValue) { char[] hexValue = new char[2]; hexValue[1] = lookup_hex[bValue&0x0F]; hexValue[0] = lookup_hex[(bValue>>4)&0x0F]; return new String(hexValue); } /** * Converts a char array into a hex-encoded String, using the provided * delimeter. * * @param data The char[] to convert to a hex-encoded string * @return Hex-encoded String */ public static final String toHexString(char[] data) { return toHexString(data, 0, data.length, ""); } /** * Converts a byte array into a hex-encoded String, using the provided * delimeter. * * @param data The char[] to convert to a hex-encoded string * @param offset the offset to start converting bytes * @param length the number of bytes to convert * @return Hex-encoded String */ public static final String toHexString(char[] data, int offset, int length) { return toHexString(data, offset, length, ""); } /** * Converts a char array into a hex-encoded String, using the provided * delimeter. * * @param data The char[] to convert to a hex-encoded string * @param delimeter the delimeter to place between each byte of data * @return Hex-encoded String */ public static final String toHexString(char[] data, String delimeter) { return toHexString(data, 0, data.length, delimeter); } /** * Converts a char array into a hex-encoded String, using the provided * delimeter. * * @param data The char[] to convert to a hex-encoded string * @param offset the offset to start converting bytes * @param length the number of bytes to convert * @param delimeter the delimeter to place between each byte of data * @return Hex-encoded String */ public static final String toHexString(char[] data, int offset, int length, String delimeter) { StringBuffer value = new StringBuffer(length*(2+delimeter.length())); int max = length+offset; int lastDelim = max-1; for(int i=offset; i<max; i++) { char bits = data[i]; value.append(lookup_hex[(bits>>4)&0x0F]); value.append(lookup_hex[bits&0x0F]); if(i<lastDelim) value.append(delimeter); } return value.toString(); } /** * <P>Converts a single character into a hex-encoded string.</P> * * @param bValue the byte to encode * @return String Hex-encoded String */ public static final String toHexString(char bValue) { char[] hexValue = new char[2]; hexValue[1] = lookup_hex[bValue&0x0F]; hexValue[0] = lookup_hex[(bValue>>4)&0x0F]; return new String(hexValue); } // ---------------------------------------------------------------------- // String <-> Long conversions // ---------------------------------------------------------------------- /** * <P>Converts a hex-encoded string (LSByte) into a long.</P> * <P>To illustrate the rules for parsing, the following String:<br> * "FF 0 1234 567 12 03"<br> * becomes:<br> * long 0x03120756341200ff * </P> * * @param strData hex-encoded numerical string * @return the decoded long */ public static final long toLong(String strData) { return toLong(toByteArray(strData)); } /** * <P>Converts a long into a hex-encoded string (LSByte).</P> * * @param lValue the long integer to encode * @return String Hex-encoded String */ public static final String toHexString(long lValue) { return toHexString(toByteArray(lValue),""); } // ---------------------------------------------------------------------- // String <-> Int conversions // ---------------------------------------------------------------------- /** * <P>Converts a hex-encoded string (LSByte) into an int.</P> * <P>To illustrate the rules for parsing, the following String:<br> * "FF 0 1234 567 12 03"<br> * becomes:<br> * long 0x03120756341200ff * </P> * * @param strData hex-encoded numerical string * @return the decoded int */ public static final int toInt(String strData) { return toInt(toByteArray(strData)); } /** * <P>Converts an integer into a hex-encoded string (LSByte).</P> * * @param iValue the integer to encode * @return String Hex-encoded String */ public static final String toHexString(int iValue) { return toHexString(toByteArray(iValue),""); } // ---------------------------------------------------------------------- // Double conversions // ---------------------------------------------------------------------- /** Field Double NEGATIVE_INFINITY */ static final double d_POSITIVE_INFINITY = 1.0d / 0.0d; /** Field Double NEGATIVE_INFINITY */ static final double d_NEGATIVE_INFINITY = -1.0d / 0.0d; /** * <P>Converts a double value into a string with the specified number of * digits after the decimal place.</P> * * @param dubbel the double value to convert to a string * @param nFrac the number of digits to display after the decimal point * * @return String representation of the double value with the specified * number of digits after the decimal place. */ public static final String toString(double dubbel, int nFrac) { // check for special case if(dubbel==d_POSITIVE_INFINITY) return "Infinity"; else if(dubbel==d_NEGATIVE_INFINITY) return "-Infinity"; else if(dubbel!=dubbel) return "NaN"; // check for fast out (no fractional digits) if(nFrac<=0) // round the whole portion return Long.toString((long)(dubbel + 0.5d)); // extract the non-fractional portion long dWhole = (long)dubbel; // figure out if it's positive or negative. We need to remove // the sign from the fractional part double sign = (dWhole<0) ? -1d : 1d; // figure out how many places to shift fractional portion double shifter = 1; for(int i=0; i<nFrac; i++) shifter *= 10; // extract, unsign, shift, and round the fractional portion long dFrac = (long)((dubbel - dWhole)*sign*shifter + 0.5d); // convert the fractional portion to a string String fracString = Long.toString(dFrac); int fracLength = fracString.length(); // ensure that rounding the fraction didn't carry into the whole portion if(fracLength>nFrac) { dWhole += 1; fracLength = 0; } // convert the whole portion to a string String wholeString = Long.toString(dWhole); int wholeLength = wholeString.length(); // create the string buffer char[] dubbelChars = new char[wholeLength + 1 + nFrac]; // append the non-fractional portion wholeString.getChars(0, wholeLength, dubbelChars, 0); // and the decimal place dubbelChars[wholeLength] = '.'; // append any necessary leading zeroes int i = wholeLength + 1; int max = i + nFrac - fracLength; for(; i<max; i++) dubbelChars[i] = '0'; // append the fractional portion if(fracLength>0) fracString.getChars(0, fracLength, dubbelChars, max); return new String(dubbelChars, 0, dubbelChars.length); } // ---------------------------------------------------------------------- // Float conversions // ---------------------------------------------------------------------- /** Field Float NEGATIVE_INFINITY */ static final float f_POSITIVE_INFINITY = 1.0f / 0.0f; /** Field Float NEGATIVE_INFINITY */ static final float f_NEGATIVE_INFINITY = -1.0f / 0.0f; /** * <P>Converts a float value into a string with the specified number of * digits after the decimal place.</P> * <P>Note: this function does not properly handle special case float * values such as Infinity and NaN.</P> * * @param flote the float value to convert to a string * @param nFrac the number of digits to display after the decimal point * * @return String representation of the float value with the specified * number of digits after the decimal place. */ public static final String toString(float flote, int nFrac) { // check for special case if(flote==f_POSITIVE_INFINITY) return "Infinity"; else if(flote==f_NEGATIVE_INFINITY) return "-Infinity"; else if(flote!=flote) return "NaN"; // check for fast out (no fractional digits) if(nFrac<=0) // round the whole portion return Long.toString((long)(flote + 0.5f)); // extract the non-fractional portion long fWhole = (long)flote; // figure out if it's positive or negative. We need to remove // the sign from the fractional part float sign = (fWhole<0) ? -1f : 1f; // figure out how many places to shift fractional portion float shifter = 1; for(int i=0; i<nFrac; i++) shifter *= 10; // extract, shift, and round the fractional portion long fFrac = (long)((flote - fWhole)*sign*shifter + 0.5f); // convert the fractional portion to a string String fracString = Long.toString(fFrac); int fracLength = fracString.length(); // ensure that rounding the fraction didn't carry into the whole portion if(fracLength>nFrac) { fWhole += 1; fracLength = 0; } // convert the whole portion to a string String wholeString = String.valueOf(fWhole); int wholeLength = wholeString.length(); // create the string buffer char[] floteChars = new char[wholeLength + 1 + nFrac]; // append the non-fractional portion wholeString.getChars(0, wholeLength, floteChars, 0); // and the decimal place floteChars[wholeLength] = '.'; // append any necessary leading zeroes int i = wholeLength + 1; int max = i + nFrac - fracLength; for(; i<max; i++) floteChars[i] = '0'; // append the fractional portion if(fracLength>0) fracString.getChars(0, fracLength, floteChars, max); return new String(floteChars, 0, floteChars.length); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -