? ? 今天使用zl464發(fā)送tts語音播報(bào),文檔上面明確要求中文編碼是UTF-8,但是我發(fā)過去的中文,它全都不認(rèn)識(shí),最后實(shí)驗(yàn)出來需要將字符串轉(zhuǎn)成Unicode它就認(rèn)識(shí)了,下面記錄了java中文轉(zhuǎn)Unicode的方法。
Java實(shí)現(xiàn)Unicode與普通字符的轉(zhuǎn)換
什么是Unicode?與UTF-8、UTF-16、UTF-32是什么關(guān)系?
Unicode是一個(gè)字符編碼標(biāo)準(zhǔn),負(fù)責(zé)分配某個(gè)字符在Unicode字符集中的序號。
UTF-8、UTF-16、UTF-32等則是具體的編碼方案,也就是將字符在Unicode字符集中的序號轉(zhuǎn)換為具體的編碼方案。
如:文章來源:http://www.zghlxwxcb.cn/news/detail-694342.html
UTF-8是針對不同范圍的序號轉(zhuǎn)換成不同長度的字符編碼,最短編碼為一個(gè)字節(jié)(8bit),可兼容ASCII;
UTF-16跟UTF-8類似,不過最短編碼為兩個(gè)字節(jié)(16bit),不可兼容ASCII;
當(dāng)前Unicode能容納的最大編號為2^32 - 1,也就是32bit,所以UTF-32是每個(gè)字符長度固定為32bit的定長編碼。
如何進(jìn)行轉(zhuǎn)換?
知道什么是Unicode以后,代碼就很簡單了:將字符對應(yīng)的Unicode編碼轉(zhuǎn)為16進(jìn)制,并加上\u前綴即可轉(zhuǎn)為Unicode;剝離Unicode的\u前綴即可獲得其在Unicode字符集的序號,轉(zhuǎn)成String即可。
引用原文鏈接:https://blog.csdn.net/java_t_t/article/details/127840074文章來源地址http://www.zghlxwxcb.cn/news/detail-694342.html
package com.photon.core.DataApi.Utils;
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UnicodeCharConvert {
private static final Pattern PATTERN_UNICODE = Pattern.compile("\\\\u[a-f0-9A-F]{1,4}");
/**
* unicode串轉(zhuǎn)字符串
*
* @param unicode unicode串
* @return 字符串
*/
private static String unicodeToChar(String unicode) {
if (unicode == null || unicode.isEmpty()) {
return unicode;
}
StringBuffer str = new StringBuffer();
String[] hex = unicode.split("\\\\u");
for (int index = 1; index < hex.length; index++) {
int data = Integer.parseInt(hex[index], 16);
str.append((char) data);
}
return str.toString();
}
/**
* 字符串轉(zhuǎn)unicode串
*
* @param str 字符串
* @return unicode串
*/
public static String charToUnicode(String str) {
if (str == null || str.isEmpty()) {
return str;
}
StringBuffer unicode = new StringBuffer();
for (int index = 0; index < str.length(); index++) {
char c = str.charAt(index);
// 轉(zhuǎn)換為unicode
String tmp = Integer.toHexString(c);
if (tmp.length() >= 4) {
unicode.append("\\u" + tmp);
} else if (tmp.length() == 3) {
unicode.append("\\u0" + tmp);
} else if (tmp.length() == 2) {
unicode.append("\\u00" + tmp);
} else if (tmp.length() == 1) {
unicode.append("\\u000" + tmp);
} else if (tmp.length() == 3) {
unicode.append("\\u0000");
}
}
return unicode.toString();
}
/**
* 混合串轉(zhuǎn)普通字符串
* 混合串指的是包含unicode和普通字符的字符串
*
* @param mixStr 混合串
* @return 普通字符串
*/
public static String mixStrToString(String mixStr) {
if (mixStr == null || mixStr.isEmpty()) {
return mixStr;
}
int start = 0;
StringBuffer result = new StringBuffer();
Matcher matcher = PATTERN_UNICODE.matcher(mixStr);
while (matcher.find()) {
String oldChar = matcher.group();
result.append(mixStr.substring(start, matcher.start()));
result.append(unicodeToChar(oldChar));
start = matcher.start() + oldChar.length();
}
result.append(mixStr.substring(start));
return result.toString();
}
/**
* 混合串轉(zhuǎn)unicode串
* 混合串指的是包含unicode和普通字符的字符串
*
* @param mixStr 混合串
* @return unicode串
*/
public static String mixStrToUnicode(String mixStr) {
if (mixStr == null || mixStr.isEmpty()) {
return mixStr;
}
int start = 0;
StringBuffer result = new StringBuffer();
Matcher matcher = PATTERN_UNICODE.matcher(mixStr);
while (matcher.find()) {
String oldChar = matcher.group();
result.append(charToUnicode(mixStr.substring(start, matcher.start())));
result.append(oldChar);
start = matcher.start() + oldChar.length();
}
result.append(charToUnicode(mixStr.substring(start)));
return result.toString();
}
/**
* 字符串轉(zhuǎn)換unicode,不能轉(zhuǎn)換符號
*/
public static String string2Unicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
// 取出每一個(gè)字符
char c = string.charAt(i);
if (c < 0x20 || c > 0x7E) {
// 轉(zhuǎn)換為unicode
String tmp = Integer.toHexString(c);
if (tmp.length() >= 4) {
unicode.append("\\u" + Integer.toHexString(c));
} else if (tmp.length() == 3) {
unicode.append("\\u0" + Integer.toHexString(c));
} else if (tmp.length() == 2) {
unicode.append("\\u00" + Integer.toHexString(c));
} else if (tmp.length() == 1) {
unicode.append("\\u000" + Integer.toHexString(c));
} else if (tmp.length() == 3) {
unicode.append("\\u0000");
}
} else {
unicode.append(c);
}
}
return unicode.toString();
}
public static void main(String[] args) {
Map<String, Object> v = new HashMap<>();
// Channel channel = channelMap.get(IMEI);
v.put("tts", UnicodeCharConvert.charToUnicode("您好,您的訂單即將結(jié)束,剩余時(shí)間15分鐘,如需延時(shí),請盡快續(xù)約!"));
v.put("vol", 100);
String strPacket = JSON.toJSONString(v);
System.out.println(msg);
System.out.println(strPacket);
//map 轉(zhuǎn)成json 后會(huì)有多余的反斜杠需要去掉
System.out.println(strPacket.replace("\\\\", "\\"));
}
}
到了這里,關(guān)于java 實(shí)現(xiàn)Unicode與普通字符(包括中文)的轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!