字符集异常
parent
6b18f6db1c
commit
2d6a6b0d80
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.common.core.text;
|
||||
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
@ -15,6 +16,7 @@ import java.util.Set;
|
|||
*
|
||||
* @author muyu
|
||||
*/
|
||||
@Log4j2
|
||||
public class Convert {
|
||||
/**
|
||||
* 转换为字符串<br>
|
||||
|
@ -23,10 +25,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static String toStr (Object value, String defaultValue) {
|
||||
public static String toStr(Object value, String defaultValue) {
|
||||
if (null == value) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -42,10 +43,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static String toStr (Object value) {
|
||||
public static String toStr(Object value) {
|
||||
return toStr(value, null);
|
||||
}
|
||||
|
||||
|
@ -56,10 +56,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Character toChar (Object value, Character defaultValue) {
|
||||
public static Character toChar(Object value, Character defaultValue) {
|
||||
if (null == value) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -77,10 +76,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Character toChar (Object value) {
|
||||
public static Character toChar(Object value) {
|
||||
return toChar(value, null);
|
||||
}
|
||||
|
||||
|
@ -91,10 +89,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Byte toByte (Object value, Byte defaultValue) {
|
||||
public static Byte toByte(Object value, Byte defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -121,10 +118,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Byte toByte (Object value) {
|
||||
public static Byte toByte(Object value) {
|
||||
return toByte(value, null);
|
||||
}
|
||||
|
||||
|
@ -135,10 +131,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Short toShort (Object value, Short defaultValue) {
|
||||
public static Short toShort(Object value, Short defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -165,10 +160,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Short toShort (Object value) {
|
||||
public static Short toShort(Object value) {
|
||||
return toShort(value, null);
|
||||
}
|
||||
|
||||
|
@ -179,10 +173,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Number toNumber (Object value, Number defaultValue) {
|
||||
public static Number toNumber(Object value, Number defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -206,10 +199,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Number toNumber (Object value) {
|
||||
public static Number toNumber(Object value) {
|
||||
return toNumber(value, null);
|
||||
}
|
||||
|
||||
|
@ -220,10 +212,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Integer toInt (Object value, Integer defaultValue) {
|
||||
public static Integer toInt(Object value, Integer defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -250,10 +241,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Integer toInt (Object value) {
|
||||
public static Integer toInt(Object value) {
|
||||
return toInt(value, null);
|
||||
}
|
||||
|
||||
|
@ -261,10 +251,9 @@ public class Convert {
|
|||
* 转换为Integer数组<br>
|
||||
*
|
||||
* @param str 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Integer[] toIntArray (String str) {
|
||||
public static Integer[] toIntArray(String str) {
|
||||
return toIntArray(",", str);
|
||||
}
|
||||
|
||||
|
@ -272,10 +261,9 @@ public class Convert {
|
|||
* 转换为Long数组<br>
|
||||
*
|
||||
* @param str 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Long[] toLongArray (String str) {
|
||||
public static Long[] toLongArray(String str) {
|
||||
return toLongArray(",", str);
|
||||
}
|
||||
|
||||
|
@ -284,16 +272,15 @@ public class Convert {
|
|||
*
|
||||
* @param split 分隔符
|
||||
* @param str 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Integer[] toIntArray (String split, String str) {
|
||||
public static Integer[] toIntArray(String split, String str) {
|
||||
if (StringUtils.isEmpty(str)) {
|
||||
return new Integer[]{};
|
||||
}
|
||||
String[] arr = str.split(split);
|
||||
final Integer[] ints = new Integer[arr.length];
|
||||
for (int i = 0 ; i < arr.length ; i++) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
final Integer v = toInt(arr[i], 0);
|
||||
ints[i] = v;
|
||||
}
|
||||
|
@ -305,16 +292,15 @@ public class Convert {
|
|||
*
|
||||
* @param split 分隔符
|
||||
* @param str 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Long[] toLongArray (String split, String str) {
|
||||
public static Long[] toLongArray(String split, String str) {
|
||||
if (StringUtils.isEmpty(str)) {
|
||||
return new Long[]{};
|
||||
}
|
||||
String[] arr = str.split(split);
|
||||
final Long[] longs = new Long[arr.length];
|
||||
for (int i = 0 ; i < arr.length ; i++) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
final Long v = toLong(arr[i], null);
|
||||
longs[i] = v;
|
||||
}
|
||||
|
@ -325,10 +311,9 @@ public class Convert {
|
|||
* 转换为String数组<br>
|
||||
*
|
||||
* @param str 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static String[] toStrArray (String str) {
|
||||
public static String[] toStrArray(String str) {
|
||||
return toStrArray(",", str);
|
||||
}
|
||||
|
||||
|
@ -337,10 +322,9 @@ public class Convert {
|
|||
*
|
||||
* @param split 分隔符
|
||||
* @param str 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static String[] toStrArray (String split, String str) {
|
||||
public static String[] toStrArray(String split, String str) {
|
||||
return str.split(split);
|
||||
}
|
||||
|
||||
|
@ -351,10 +335,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Long toLong (Object value, Long defaultValue) {
|
||||
public static Long toLong(Object value, Long defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -382,10 +365,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Long toLong (Object value) {
|
||||
public static Long toLong(Object value) {
|
||||
return toLong(value, null);
|
||||
}
|
||||
|
||||
|
@ -396,10 +378,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Double toDouble (Object value, Double defaultValue) {
|
||||
public static Double toDouble(Object value, Double defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -427,10 +408,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Double toDouble (Object value) {
|
||||
public static Double toDouble(Object value) {
|
||||
return toDouble(value, null);
|
||||
}
|
||||
|
||||
|
@ -441,10 +421,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Float toFloat (Object value, Float defaultValue) {
|
||||
public static Float toFloat(Object value, Float defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -471,10 +450,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Float toFloat (Object value) {
|
||||
public static Float toFloat(Object value) {
|
||||
return toFloat(value, null);
|
||||
}
|
||||
|
||||
|
@ -485,10 +463,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Boolean toBool (Object value, Boolean defaultValue) {
|
||||
public static Boolean toBool(Object value, Boolean defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -521,10 +498,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static Boolean toBool (Object value) {
|
||||
public static Boolean toBool(Object value) {
|
||||
return toBool(value, null);
|
||||
}
|
||||
|
||||
|
@ -535,10 +511,9 @@ public class Convert {
|
|||
* @param clazz Enum的Class
|
||||
* @param value 值
|
||||
* @param defaultValue 默认值
|
||||
*
|
||||
* @return Enum
|
||||
*/
|
||||
public static <E extends Enum<E>> E toEnum (Class<E> clazz, Object value, E defaultValue) {
|
||||
public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -564,10 +539,9 @@ public class Convert {
|
|||
*
|
||||
* @param clazz Enum的Class
|
||||
* @param value 值
|
||||
*
|
||||
* @return Enum
|
||||
*/
|
||||
public static <E extends Enum<E>> E toEnum (Class<E> clazz, Object value) {
|
||||
public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) {
|
||||
return toEnum(clazz, value, null);
|
||||
}
|
||||
|
||||
|
@ -578,10 +552,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static BigInteger toBigInteger (Object value, BigInteger defaultValue) {
|
||||
public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -608,10 +581,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static BigInteger toBigInteger (Object value) {
|
||||
public static BigInteger toBigInteger(Object value) {
|
||||
return toBigInteger(value, null);
|
||||
}
|
||||
|
||||
|
@ -622,10 +594,9 @@ public class Convert {
|
|||
*
|
||||
* @param value 被转换的值
|
||||
* @param defaultValue 转换错误时的默认值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static BigDecimal toBigDecimal (Object value, BigDecimal defaultValue) {
|
||||
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -658,10 +629,9 @@ public class Convert {
|
|||
* 转换失败不会报错
|
||||
*
|
||||
* @param value 被转换的值
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static BigDecimal toBigDecimal (Object value) {
|
||||
public static BigDecimal toBigDecimal(Object value) {
|
||||
return toBigDecimal(value, null);
|
||||
}
|
||||
|
||||
|
@ -670,10 +640,9 @@ public class Convert {
|
|||
* 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
|
||||
*
|
||||
* @param obj 对象
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String utf8Str (Object obj) {
|
||||
public static String utf8Str(Object obj) {
|
||||
return str(obj, CharsetKit.CHARSET_UTF_8);
|
||||
}
|
||||
|
||||
|
@ -683,11 +652,15 @@ public class Convert {
|
|||
*
|
||||
* @param obj 对象
|
||||
* @param charsetName 字符集
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String str (Object obj, String charsetName) {
|
||||
return str(obj, Charset.forName(charsetName));
|
||||
public static String str(Object obj, String charsetName) {
|
||||
try {
|
||||
return str(obj, Charset.forName(charsetName));
|
||||
} catch (Exception exception) {
|
||||
log.error("字符转换异常, [{}-{}] -> {}", obj, charsetName, exception.getMessage(), exception);
|
||||
throw new RuntimeException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -696,10 +669,9 @@ public class Convert {
|
|||
*
|
||||
* @param obj 对象
|
||||
* @param charset 字符集
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String str (Object obj, Charset charset) {
|
||||
public static String str(Object obj, Charset charset) {
|
||||
if (null == obj) {
|
||||
return null;
|
||||
}
|
||||
|
@ -713,7 +685,7 @@ public class Convert {
|
|||
Byte[] bytes = (Byte[]) obj;
|
||||
int length = bytes.length;
|
||||
byte[] dest = new byte[length];
|
||||
for (int i = 0 ; i < length ; i++) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
dest[i] = bytes[i];
|
||||
}
|
||||
return str(dest, charset);
|
||||
|
@ -729,10 +701,9 @@ public class Convert {
|
|||
*
|
||||
* @param bytes byte数组
|
||||
* @param charset 字符集
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String str (byte[] bytes, String charset) {
|
||||
public static String str(byte[] bytes, String charset) {
|
||||
return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
|
||||
}
|
||||
|
||||
|
@ -741,10 +712,9 @@ public class Convert {
|
|||
*
|
||||
* @param data 字符串
|
||||
* @param charset 字符集,如果此字段为空,则解码的结果取决于平台
|
||||
*
|
||||
* @return 解码后的字符串
|
||||
*/
|
||||
public static String str (byte[] data, Charset charset) {
|
||||
public static String str(byte[] data, Charset charset) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -760,10 +730,9 @@ public class Convert {
|
|||
*
|
||||
* @param data 数据
|
||||
* @param charset 字符集,如果为空使用当前系统字符集
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String str (ByteBuffer data, String charset) {
|
||||
public static String str(ByteBuffer data, String charset) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -776,10 +745,9 @@ public class Convert {
|
|||
*
|
||||
* @param data 数据
|
||||
* @param charset 字符集,如果为空使用当前系统字符集
|
||||
*
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String str (ByteBuffer data, Charset charset) {
|
||||
public static String str(ByteBuffer data, Charset charset) {
|
||||
if (null == charset) {
|
||||
charset = Charset.defaultCharset();
|
||||
}
|
||||
|
@ -792,10 +760,9 @@ public class Convert {
|
|||
* 半角转全角
|
||||
*
|
||||
* @param input String.
|
||||
*
|
||||
* @return 全角字符串.
|
||||
*/
|
||||
public static String toSBC (String input) {
|
||||
public static String toSBC(String input) {
|
||||
return toSBC(input, null);
|
||||
}
|
||||
|
||||
|
@ -804,12 +771,11 @@ public class Convert {
|
|||
*
|
||||
* @param input String
|
||||
* @param notConvertSet 不替换的字符集合
|
||||
*
|
||||
* @return 全角字符串.
|
||||
*/
|
||||
public static String toSBC (String input, Set<Character> notConvertSet) {
|
||||
public static String toSBC(String input, Set<Character> notConvertSet) {
|
||||
char[] c = input.toCharArray();
|
||||
for (int i = 0 ; i < c.length ; i++) {
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (null != notConvertSet && notConvertSet.contains(c[i])) {
|
||||
// 跳过不替换的字符
|
||||
continue;
|
||||
|
@ -829,10 +795,9 @@ public class Convert {
|
|||
* 全角转半角
|
||||
*
|
||||
* @param input String.
|
||||
*
|
||||
* @return 半角字符串
|
||||
*/
|
||||
public static String toDBC (String input) {
|
||||
public static String toDBC(String input) {
|
||||
return toDBC(input, null);
|
||||
}
|
||||
|
||||
|
@ -841,12 +806,11 @@ public class Convert {
|
|||
*
|
||||
* @param text 文本
|
||||
* @param notConvertSet 不替换的字符集合
|
||||
*
|
||||
* @return 替换后的字符
|
||||
*/
|
||||
public static String toDBC (String text, Set<Character> notConvertSet) {
|
||||
public static String toDBC(String text, Set<Character> notConvertSet) {
|
||||
char[] c = text.toCharArray();
|
||||
for (int i = 0 ; i < c.length ; i++) {
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
if (null != notConvertSet && notConvertSet.contains(c[i])) {
|
||||
// 跳过不替换的字符
|
||||
continue;
|
||||
|
@ -865,10 +829,9 @@ public class Convert {
|
|||
* 数字金额大写转换 先写个完整的然后将如零拾替换成零
|
||||
*
|
||||
* @param n 数字
|
||||
*
|
||||
* @return 中文大写数字
|
||||
*/
|
||||
public static String digitUppercase (double n) {
|
||||
public static String digitUppercase(double n) {
|
||||
String[] fraction = {"角", "分"};
|
||||
String[] digit = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
||||
String[][] unit = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};
|
||||
|
@ -877,7 +840,7 @@ public class Convert {
|
|||
n = Math.abs(n);
|
||||
|
||||
String s = "";
|
||||
for (int i = 0 ; i < fraction.length ; i++) {
|
||||
for (int i = 0; i < fraction.length; i++) {
|
||||
// 优化double计算精度丢失问题
|
||||
BigDecimal nNum = new BigDecimal(n);
|
||||
BigDecimal decimal = new BigDecimal(10);
|
||||
|
@ -890,9 +853,9 @@ public class Convert {
|
|||
}
|
||||
int integerPart = (int) Math.floor(n);
|
||||
|
||||
for (int i = 0 ; i < unit[0].length && integerPart > 0 ; i++) {
|
||||
for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
|
||||
String p = "";
|
||||
for (int j = 0 ; j < unit[1].length && n > 0 ; j++) {
|
||||
for (int j = 0; j < unit[1].length && n > 0; j++) {
|
||||
p = digit[integerPart % 10] + unit[1][j] + p;
|
||||
integerPart = integerPart / 10;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue