字符串转化 nicode utf8 转化汉字

/**
* 字符串转化为unicode
* @param gbString
* @return
*/
public static String encodeUnicode(final String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
String hexB = Integer.toHexString(utfBytes[byteIndex]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
//unicodeBytes = unicodeBytes + "\\u" + hexB;
unicodeBytes = unicodeBytes + hexB;
}
System.out.println(unicodeBytes);
return unicodeBytes;
}

// unicode转化汉字
public static StringBuffer decodeUnicode(String utfStr) {
final StringBuffer buffer = new StringBuffer();
String charStr = "";
String operStr = utfStr;
for(int i =0 ; i < utfStr.length() ;i=+4){
charStr = operStr.substring(0, 4);
operStr = operStr.substring(4, operStr.length());
char letter = (char) Integer.parseInt(charStr, 16);
buffer.append(new Character(letter).toString());
}
return buffer;
}
/**
* 信息按长度截取
* @param messages
* @param length 截取长度
* @param vmsg
* @return
*/
public static Vector<String> splitString(String messages, int length, Vector<String> vmsg) {
if (messages.length() <= length) {
vmsg.add(messages);
} else {
vmsg.add(messages.substring(0, length));
messages = messages.substring(length);
vmsg = splitString(messages,length,vmsg);
}

return vmsg;
}
/**
* 用utf-8格式保存文件
* @param fileName
* @param msg
*/
public static void outputFile(String fileName, String msg) {
File file = getFileByFileName(fileName);
Writer ow = null;
try {
// FileWriter fw = new FileWriter(file);
// fw.write(msg);
// fw.flush();
// byte[] bytes = msg.getBytes("GBK");
// String str = new String(bytes, "GBK");
// OutputStream os = new FileOutputStream(file);
// os.write(bytes);
// os.close();
ow = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
ow.write(msg);
} catch (IOException e) {
System.out.println(e);
} finally {
//关闭reader
if (null != ow) {
try {
ow.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值