public class TextUtil {
- private static final char[] HEX = {
- '0','1','2','3','4','5','6','7',
- '8','9','a','b','c','d','e','f'
+ private static final char[] HEX = {
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
-
+
/**
* Return the bytes formatted as a hexadecimal string. The length of the
*/
public static final String hexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
- for (byte b: bytes) {
+ for (byte b : bytes) {
sb.append(HEX[(b >>> 4) & 0xF]);
sb.append(HEX[b & 0xF]);
}
return sb.toString();
}
-
+
/**
* Return a string of the double value with suitable precision (5 digits).
* The string is the shortest representation of the value including the
return "Inf";
}
-
+
final String sign = (d < 0) ? "-" : "";
double abs = Math.abs(d);
*/
private static String decimalFormat(double value) {
if (value >= 10000)
- return "" + (int)(value + 0.5);
+ return "" + (int) (value + 0.5);
int decimals = 1;
double v = value;
}
-
-
+
+
/*
* value must be positive!
*/
// Calculate rounding and limit values (rounding slightly smaller)
int rounding = 1;
double limit = 0.5;
- for (int i=0; i<decimals; i++) {
+ for (int i = 0; i < decimals; i++) {
rounding *= 10;
limit /= 10;
}
-
- // Round value
- value = (Math.rint(value * rounding)+0.1) / rounding;
+ // Round value
+ value = (Math.rint(value * rounding) + 0.1) / rounding;
- int whole = (int)value;
+
+ int whole = (int) value;
value -= whole;
-
+
if (value < limit)
- return "" + whole;
+ return "" + whole;
limit *= 10;
-
+
StringBuilder sb = new StringBuilder();
sb.append("" + whole);
sb.append('.');
-
- for (int i = 0; i<decimals; i++) {
+
+ for (int i = 0; i < decimals; i++) {
value *= 10;
- whole = (int)value;
+ whole = (int) value;
value -= whole;
- sb.append((char)('0' + whole));
+ sb.append((char) ('0' + whole));
if (value < limit)
return sb.toString();
limit *= 10;
-
+
}
-
+
return sb.toString();
}
-
+
+
+ public static String htmlEncode(String s) {
+ s = s.replace("&", "&");
+ s = s.replace("\"", """);
+ s = s.replace("<", "<");
+ s = s.replace(">", ">");
+ return s;
+ }
}