在实习的时候,做了一个关于发送短信的接口,营运商提供一个url,且信息如有中文字符的,必须用 DCS 8的格式,当时懵了。。DSC是什么鬼。。后来百度了一大堆资料DSC 8就是用UCS格式做的和咨询M800公司的工程师,才得以解决。。。在编译文字这里花费了好长的时间。大概了解UTF-8 与UCS2的网站有http://www.52rd.com/Blog/Detail_RD.Blog_thieven_4865.html。基层的基本不写了。底层的转换源码如下:
代码:
package com.sms.utils.sendMsg.m800; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; /** * @author Jim Weng<br> * UCS2编码转换 * */ public class Dcs8ofM800 { /**UCS2解码*/ public static String DecodeUCS2(String src) { String reValue; byte[] bytes = new byte[src.length() / 2]; for (int i = 0; i < src.length(); i += 2) { bytes[i / 2] = (byte) (Integer .parseInt(src.substring(i, i + 2), 16)); } try { reValue = new String(bytes, "UTF-16BE"); return reValue; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /**UCS2编码*/ public static String EncodeUCS2(String src) { StringBuffer reValue = new StringBuffer(); StringBuffer tem = new StringBuffer(); byte[] bytes; try { bytes = src.getBytes("UTF-16BE"); for (int i = 0; i < bytes.length; i++) { tem.delete(0, tem.length()); tem.append(Integer.toHexString(bytes[i] & 0xFF)); if(tem.length()==1){ tem.insert(0, '0'); } reValue.append(tem); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return reValue.toString().toUpperCase(); } /* public static String chinaToUnicode(String str){ String result=""; for (int i = 0; i < str.length(); i++){ int chr1 = (char) str.charAt(i); if(chr1>=19968&&chr1<=171941){//汉字范围 u4e00-u9fa5 (中文) result+="\u" + Integer.toHexString(chr1); }else{ result+=str.charAt(i); } } return result; }*/ public static void main(String[] args) throws UnsupportedEncodingException { // System.out.println(DecodeUCS2("4F6060F3776189C94E8654275")); // System.out.println( EncodeUCS2(test) ); } }