字符编码工具类

字符编码工具类

package charTools;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;

import java.security.*;
import java.text.*;
import java.util.*;
/**
* Title:字符编码工具类
* Description:
* Copyright: flashman.com.cn Copyright (c) 2005
* Company: flashman.com.cn
* @author: jeffzhu
* @version 1.0
*/

/*
*===================功能说明====================================
*
*================================字符操作=======================
*String chopAtWord(String string, int length) 从字符串第一位开始取n位字符
*
*
*
*=================================编码转换======================
*String ISO2GB(String text) 转换编码 ISO-8859-1到GB2312
*String GB2ISO(String text) 转换编码 GB2312到ISO-8859-1
*String Utf8URLencode(String text) Utf8URL编码
*String Utf8URLdecode(String text) Utf8URL解码
*String CodeToWord(String text) utf8URL编码转字符
*boolean Utf8codeCheck(String text) 编码是否有效
*boolean isUtf8Url(String text) 是否Utf8Url编码
*
*
*==================================加密解密======================
*synchronized static final String hash(String data) MessageDigest加密
*String encodeBase64(String data) base64加密
*public static String decodeBase64(String data) base64解密
*
*
*/
public class charTools1 {
/**
* 转换编码 ISO-8859-1到GB2312
* @param text
* @return
*/
public 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 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 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 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 String CodeToWord(String text) {
String result;

if (Utf8codeCheck(text)) {
byte[] code = new byte[3];
code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
try {
result = new String(code, "UTF-8");
}catch (UnsupportedEncodingException ex) {
result = null;
}
}
else {
result = text;
}

return result;
}

public static boolean isValidUtf8(byte[] b, int aMaxCount) {
int lLen = b.length, lCharCount = 0;
for (int i = 0; i < lLen && lCharCount < aMaxCount; ++lCharCount) {
byte lByte = b[i++]; //to fast operation, ++ now, ready for the following for(;;)
if (lByte >= 0) continue; //>=0 is normal ascii
if (lByte < (byte) 0xc0 || lByte > (byte) 0xfd)
return false;
int lCount = lByte > (byte) 0xfc ? 5 : lByte > (byte) 0xf8 ? 4 : lByte > (byte) 0xf0 ? 3 : lByte > (byte) 0xe0 ? 2 : 1;
if (i + lCount > lLen) return false;
for (int j = 0; j < lCount; ++j, ++i)
if (b[i] >= (byte) 0xc0)return false;
}
return true;
}

/**
* 编码是否有效
* @param text
* @return
*/
private boolean Utf8codeCheck(String text){
String sign = "";
if (text.startsWith("%e"))
for (int i = 0, p = 0; p != -1; i++) {
p = text.indexOf("%", p);
if (p != -1)
p++;
sign += p;
}
return sign.equals("147-1");
}

/**
* 是否Utf8Url编码
* @param text
* @return
*/
public boolean isUtf8Url(String text) {
text = text.toLowerCase();
int p = text.indexOf("%");
if (p != -1 && text.length() - p > 9) {
text = text.substring(p, p + 9);
}
return Utf8codeCheck(text);
}

/*
* ======================================加密解密=================================================
*/
/**
* Used by the hash method.
*/
private static MessageDigest digest = null;

/**
* 将传入的参数转化为它所对应的hash码
* @param data the String to compute the hash of.
* @return a hashed version of the passed-in String
*/
public synchronized static final String hash(String data) {
if (digest == null) {
try {
digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException nsae) {
System.err.println("Failed to load the MD5 MessageDigest. " +
"Jive will be unable to function normally.");
nsae.printStackTrace();
}
}
// Now, compute hash.
digest.update(data.getBytes());
return encodeHex(digest.digest());
}

/**
* 将传入的byte型数组转化为对应的十六进制,并组合成字符串输出
* Turns an array of bytes into a String representing each byte as an
* unsigned hex number.
* <p>
* Method by Santeri Paavolainen, Helsinki Finland 1996<br>
* (c) Santeri Paavolainen, Helsinki Finland 1996<br>
* Distributed under LGPL.
*
* @param bytes an array of bytes to convert to a hex-string
* @return generated hex string
*/
public static final String encodeHex(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
int i;

for (i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
}
return buf.toString();
}

/**
* 将十六进制数字型的字符串转化为byte型的数组,将字符串按两位两位分开
* Turns a hex encoded string into a byte array. It is specifically meant
* to "reverse" the toHex(byte[]) method.
* @param hex a hex encoded String to transform into a byte array.
* @return a byte array representing the hex String[
*/
public static final byte[] decodeHex(String hex) {
char [] chars = hex.toCharArray();
byte[] bytes = new byte[chars.length/2];
int byteCount = 0;
for (int i=0; i<chars.length; i+=2) {
byte newByte = 0x00;
newByte |= hexCharToByte(chars[i]);
newByte <<= 4;
newByte |= hexCharToByte(chars[i+1]);
bytes[byteCount] = newByte;
byteCount++;
}
return bytes;
}


/**
* Returns the the byte value of a hexadecmical char (0-f). It's assumed
* that the hexidecimal chars are lower case as appropriate.
*
* @param ch a hexedicmal character (0-f)
* @return the byte value of the character (0x00-0x0F)
*/
private static final byte hexCharToByte(char ch) {
switch(ch) {
case '0': return 0x00;
case '1': return 0x01;
case '2': return 0x02;
case '3': return 0x03;
case '4': return 0x04;
case '5': return 0x05;
case '6': return 0x06;
case '7': return 0x07;
case '8': return 0x08;
case '9': return 0x09;
case 'a': return 0x0A;
case 'b': return 0x0B;
case 'c': return 0x0C;
case 'd': return 0x0D;
case 'e': return 0x0E;
case 'f': return 0x0F;
}
return 0x00;
}


//*********************************************************************
//* Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************

/**base64加密
* Encodes a String as a base64 String.
*
* @param data a String to encode.
* @return a base64 encoded String.
*/
public static String encodeBase64(String data) {
return encodeBase64(data.getBytes());
}

/**
* Encodes a byte array into a base64 String.
*
* @param data a byte array to encode.
* @return a base64 encode String.
*/
public static String encodeBase64(byte[] data) {
int c;
int len = data.length;
StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);
for (int i = 0; i < len; ++i) {
c = (data[i] >> 2) & 0x3f;
ret.append(cvt.charAt(c));
c = (data[i] << 4) & 0x3f;
if (++i < len)
c |= (data[i] >> 4) & 0x0f;

ret.append(cvt.charAt(c));
if (i < len) {
c = (data[i] << 2) & 0x3f;
if (++i < len)
c |= (data[i] >> 6) & 0x03;

ret.append(cvt.charAt(c));
}
else {
++i;
ret.append((char) fillchar);
}

if (i < len) {
c = data[i] & 0x3f;
ret.append(cvt.charAt(c));
}
else {
ret.append((char) fillchar);
}
}
return ret.toString();
}

/**base64解密
* Decodes a base64 String.
*
* @param data a base64 encoded String to decode.
* @return the decoded String.
*/
public static String decodeBase64(String data) {
return decodeBase64(data.getBytes());
}

/**
* Decodes a base64 aray of bytes.
*
* @param data a base64 encode byte array to decode.
* @return the decoded String.
*/
public static String decodeBase64(byte[] data) {
int c, c1;
int len = data.length;
StringBuffer ret = new StringBuffer((len * 3) / 4);
for (int i = 0; i < len; ++i) {
c = cvt.indexOf(data[i]);
++i;
c1 = cvt.indexOf(data[i]);
c = ((c << 2) | ((c1 >> 4) & 0x3));
ret.append((char) c);
if (++i < len) {
c = data[i];
if (fillchar == c)
break;

c = cvt.indexOf((char) c);
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.append((char) c1);
}

if (++i < len) {
c1 = data[i];
if (fillchar == c1)
break;

c1 = cvt.indexOf((char) c1);
c = ((c << 6) & 0xc0) | c1;
ret.append((char) c);
}
}
return ret.toString();
}

private static final int fillchar = '=';
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";



/**
* 从字符串中取n位字符
*
* @param string the String to chop.
* @param length the index in <code>string</code> to start looking for a
* whitespace boundary at.
* @return a substring of <code>string</code> whose length is less than or
* equal to <code>length</code>, and that is chopped at whitespace.
*/

public static final String chopAtWord(String string, int length) {
if (string == null) {
return string;
}

char [] charArray = string.toCharArray();
int sLength = string.length();
if (length < sLength) {
sLength = length;
}

// First check if there is a newline character before length; if so,
// chop word there.
for (int i=0; i<sLength-1; i++) {
// Windows
if (charArray[i] == '\r' && charArray[i+1] == '\n') {
return string.substring(0, i+1);
}
// Unix
else if (charArray[i] == '\n') {
return string.substring(0, i);
}
}
// Also check boundary case of Unix newline
if (charArray[sLength-1] == '\n') {
return string.substring(0, sLength-1);
}

// Done checking for newline, now see if the total string is less than
// the specified chop point.
if (string.length() < length) {
return string;
}

// No newline, so chop at the first whitespace.
for (int i = length-1; i > 0; i--) {
if (charArray[i] == ' ') {
return string.substring(0, i).trim();
}
}

// Did not find word boundary so return original String chopped at
// specified length.
return string.substring(0, length);
}




/**
* 测试
* @param args
*/
public static void main(String[] args) {
//实例化工具类
charTools1 charTools = new charTools1();

String url;
//定义一个URL字符串
url="http://www.baidu.com/s?bs=indexof+%D3%C3%B7%A8&f=8&wd=java+%D7%D6%B7%FB%B4%A6%C0%ED%B9%A4%BE%DF%C0%E0";
// url = "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91%E5%9C%A8%E7%BA%BF%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2&btnG=%E6%90%9C%E7%B4%A2&lr=";
//调用方法isUtf8Url,进行判断是否Utf8Url编码
if(charTools.isUtf8Url(url)){
//如果是Utf8Url编码则调用Utf8URLdecode进行解码
System.out.println(charTools.Utf8URLdecode(url));
}else{
System.out.println(URLDecoder.decode(url));
}

url = "http://www.baidu.com/baidu?word=%D6%D0%B9%FA%B4%F3%B0%D9%BF%C6%D4%DA%CF%DF%C8%AB%CE%C4%BC%EC%CB%F7&tn=myie2dg";
if(charTools.isUtf8Url(url)){
System.out.println(charTools.Utf8URLdecode(url));
}else{
System.out.println(URLDecoder.decode(url));
}

String charT="刘奇庭是不错的人";
String ct=charTools1.chopAtWord(charT, 5);
System.out.println(ct);


}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java字符编码转换的工具类可以通过使用Java自带的Charset类和String类的getBytes()方法来实现。下面是一个示例的工具类: ```java import java.nio.charset.Charset; public class EncodingUtils { // 将指定编码字符串转换为默认编码字符串 public static String convertToDefaultEncoding(String str, String sourceEncoding) { byte[] bytes = str.getBytes(Charset.forName(sourceEncoding)); return new String(bytes); } // 将默认编码字符串转换为指定编码字符串 public static String convertToEncoding(String str, String targetEncoding) { byte[] bytes = str.getBytes(); return new String(bytes, Charset.forName(targetEncoding)); } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { String str1 = "中文字符串"; // 将UTF-8编码字符串转换为默认编码字符串 String defaultEncodingStr = EncodingUtils.convertToDefaultEncoding(str1, "UTF-8"); System.out.println("Default Encoding: " + defaultEncodingStr); // 将默认编码字符串转换为GBK编码字符串 String targetEncodingStr = EncodingUtils.convertToEncoding(defaultEncodingStr, "GBK"); System.out.println("Target Encoding: " + targetEncodingStr); } } ``` 在上述示例中,工具类`EncodingUtils`提供了两个静态方法。`convertToDefaultEncoding()`方法将指定编码字符串转换为默认编码字符串,而`convertToEncoding()`方法将默认编码字符串转换为指定编码字符串。 ### 回答2: Java字符编码转换工具类是一种用于处理字符编码转换的工具类,它可以将字符串从一种字符编码转换为另一种字符编码。在Java中,字符串的编码可以使用标准的UTF-8、UTF-16、ISO-8859-1等编码方式。 这个工具类通常提供以下几种方法: 1. `toUTF8(String str)`:将字符串从其他编码转换为UTF-8编码。 2. `fromUTF8(String str)`:将UTF-8编码字符串转换为其他编码。 3. `toISO88591(String str)`:将字符串从其他编码转换为ISO-8859-1编码。 4. `fromISO88591(String str)`:将ISO-8859-1编码字符串转换为其他编码。 使用这个工具类可以避免在转换编码时出现乱码或者字符串不可识别的问题。例如,当我们从外部资源读取数据时,如果字符串的编码与程序默认字符编码不一致,就会导致乱码,这时可以使用这个工具类进行编码转换。 在实现这个工具类时,可以使用Java提供的相关类库,如`java.nio.charset.Charset`、`java.nio.CharBuffer`等类来进行编码转换操作。首先,通过指定源编码和目标编码,创建`Charset`对象;然后,使用`encode`方法将源字符编码成字节序列,并使用目标`Charset`对象的`decode`方法将字节序列解码成目标编码字符串。 使用这个工具类时,需要注意的是源编码和目标编码必须是支持的字符编码,否则会抛出编码不支持的异常。此外,还应该注意对输入的异常情况进行处理,例如空字符串或空指针异常,以确保程序的健壮性。 总之,Java字符编码转换工具类是一个方便实用的工具,可以帮助我们在不同编码间进行转换,避免乱码的问题,提高程序的稳定性和可靠性。 ### 回答3: Java提供了许多内置的工具类来方便字符编码转换。其中最常用的工具类是`java.nio.charset.Charset`和`java.lang.String`类。 首先,`Charset`类包含了许多常见的字符,比如UTF-8、GBK、ISO-8859-1等。我们可以使用`Charset.forName(String charsetName)`方法来获取指定字符的一个实例。 接下来,`String`类提供了几个方法来进行字符编码转换。其中最常用的是`getBytes(String charsetName)`方法,它将字符串按照指定的字符转换为字节数组。例如,如果我们想将字符串转换为UTF-8编码的字节数组,可以使用`getBytes("UTF-8")`方法。 除了将字符串转换为字节数组,`String`类还提供了`getBytes()`方法,它将字符串按照默认的字符转换为字节数组。默认的字符可以通过调用`Charset.defaultCharset()`方法获取。 另外,如果我们想将字节数组转换为字符串,可以使用`String`类的构造方法`String(byte[] bytes, Charset charset)`。该构造方法将字节数组按照指定的字符转换为字符串。 除了上述的方法,还有一些其他的工具类可以辅助字符编码转换。比如,`java.io.InputStreamReader`和`java.io.OutputStreamWriter`类提供了将字节流与字符流进行转换的功能。 总结来说,Java提供了丰富的工具类来进行字符编码转换。我们可以通过`Charset`类获取指定字符的实例,通过`String`类的相关方法来进行字符串与字节数组之间的转换。另外,还可以使用`java.io.InputStreamReader`和`java.io.OutputStreamWriter`类进行字节流与字符流之间的转换。这些工具类的使用可以方便地实现字符编码转换的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值