现在的位置: 首页 > 综合 > 正文

字符串转Ascii码与Ascii码转字符串

2018年04月22日 ⁄ 综合 ⁄ 共 534字 ⁄ 字号 评论关闭

字符串转Ascii码

private static String convert(String str) {

String tmp; 
StringBuffer sb = new StringBuffer(1000); 
char c; 
int i, j; 
sb.setLength(0); 
for(i = 0;i<str.length();i++){ 
c = str.charAt(i); 
if (c > 255) { 
sb.append("\\u"); 
j = (c >>> 8); 
tmp = Integer.toHexString(j); 
if (tmp.length() == 1) sb.append("0"); 
sb.append(tmp); 
j = (c & 0xFF); 
tmp = Integer.toHexString(j); 
if (tmp.length() == 1) sb.append("0"); 
sb.append(tmp); 

else { 
sb.append(c); 

}
return(new String(sb));

}

Ascii码转字符串

byte[] bb = (String.valueOf("\u7f16\u7801")).getBytes("UTF-8");
String cc = new String(bb, "UTF-8");

抱歉!评论已关闭.