package com.jarvis.base.util;
import java.io.UnsupportedEncodingException;
/**
* <p>
* Title:字符编码工具类
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company:
* </p>
*
* @author:
* @version 1.0
*/
public class CharTools {
/**
* 转换编码 ISO-8859-1到GB2312
*
* @param text
* @return
*/
public static final String ISO2GB(String text) {
String result = "";
try {
result = new String(text.getBytes("ISO-8859-1"), "GB2312");
} catch (UnsupportedEncodingException ex) {
result = ex.toString();
}
return result;
}
/**
* 转换编码 GB2312到ISO-8859-1
*
* @param text
* @return
*/
public static final String GB2ISO(String text) {
String result = "";
try {
result = new String(text.getBytes("GB2312"), "ISO-8859-1");
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return result;
}
/**
* Utf8URL编码
*
* @param s
* @return
*/
public static final String Utf8URLencode(String text) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 0 && c <= 255) {
result.append(c);
} else {
byte[] b = new byte[0];
try {
b = Character.toString(c).getBytes("UTF-8");
} catch (Exception ex) {
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
result.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return result.toString();
}
/**
* Utf8URL解码
*
* @param text
* @return
*/
public static final String Utf8URLdecode(String text) {
String result = "";
int p = 0;
if (text != null && text.length() > 0) {
text = text.toLowerCase();
p = text.indexOf("%e");
if (p == -1)
return text;
while (p != -1) {
result += text.substring(0, p);
text = text.substring(p, text.length());
if (text == "" || text.length() < 9)
return result;
result += CodeToWord(text.substring(0, 9));
text = text.substring(9, text.length());
p = text.indexOf("%e");
}
}
return result + text;
}
/**
* utf8URL编码转字符
*
* @param text
* @return
*/
private static final String CodeToWord(String text) {
String result;
if (Utf8codeCheck(text)) {
byte[] code = new byte[3];
code[0] = (byte) (
Java常用工具类---字符编码工具类、数据类型转换工具类
最新推荐文章于 2024-07-02 11:27:00 发布
本文详细介绍了Java中常用的字符编码工具类和数据类型转换工具类,包括它们的功能、使用场景和示例代码,帮助开发者提升代码效率和质量。
摘要由CSDN通过智能技术生成