将字符转换成ascii编码(适用于转换国际资源文件)

public class ConvertToAscii {
	private 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));
	}
    public static void main(String[] args) {
		ConvertToAscii cta = new ConvertToAscii();
		String a = cta.convert("用户ID不能为空!");
		System.out.println(a);
	}
}

你可能感兴趣的:(C++,c,C#,J#)