汉字转ASCII码

public class TestFire {
	//bytesToHexString
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return "";
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}
	public static String addRightSpaceForBytes(String str, String charset,
			int length) throws UnsupportedEncodingException {
		if (str == null) {
			str = "";
		}
		byte[] strBytes = str.getBytes(charset);
		int str_length = strBytes.length;
		for (int i = 0; i < (length - str_length); i = i + 1) {
			str = str + ' ';
		}
		return str;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String str=bytesToHexString(addRightSpaceForBytes("张三", "GBK", 40).getBytes("GBK"));
		System.out.println(str);
	}
}
发布了15 篇原创文章 · 获赞 8 · 访问量 6046

猜你喜欢

转载自blog.csdn.net/Charles_lxx/article/details/86993858