java and Chinese hexadecimal string (string) system conversion and the conversion garbled summary

/**
	 * 16进制直接转换成为字符串(无需Unicode解码)
	 * @param hexStr  Byte字符串(Byte之间无分隔符
	 * @author xxs
	 * @return 对应的字符串
	 */
	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF"; //16进制能用到的所有字符 0-15
		char[] hexs = hexStr.toCharArray();//toCharArray() 方法将字符串转换为字符数组。
		int length = (hexStr.length() / 2);//1个byte数值 -> 两个16进制字符
		byte[] bytes = new byte[length]; 
		int n;
		for (int i = 0; i < bytes.length; i++) {
			int position = i * 2;//两个16进制字符 -> 1个byte数值
			n = str.indexOf(hexs[position]) * 16;
			n += str.indexOf(hexs[position + 1]);
			// 保持二进制补码的一致性 因为byte类型字符是8bit的  而int为32bit 会自动补齐高位1  所以与上0xFF之后可以保持高位一致性 
			//当byte要转化为int的时候,高的24位必然会补1,这样,其二进制补码其实已经不一致了,&0xff可以将高的24位置为0,低8位保持原样,这样做的目的就是为了保证二进制数据的一致性。
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes);
	}
	/**
	 * 字符串转换成为16进制(无需Unicode编码)
	 * @param str 待转换的ASCII字符串
	 * @author xxs
	 * @return byte字符串 (每个Byte之间空格分隔)
	 */
	public static String str2HexStr(String str) {
		 char[] chars = "0123456789ABCDEF".toCharArray();//toCharArray() 方法将字符串转换为字符数组。
		 StringBuilder sb = new StringBuilder(""); //StringBuilder是一个类,可以用来处理字符串,sb.append()字符串相加效率高
		 byte[] bs = str.getBytes();//String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组
		 int bit;
		 for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4; // 高4位, 与操作 1111 0000
		 	sb.append(chars[bit]);
		 	bit = bs[i] & 0x0f;  // 低四位, 与操作 0000 1111
		 	sb.append(chars[bit]);
		 	sb.append(' ');//每个Byte之间空格分隔
		 }
		 return sb.toString().trim();
	}

Write a main method to test.

 

If you have a garbage problem, and that is the effect of encoding format. E.g:

Under normal circumstances, the main method to test:

But behind the browser address bar Incoming:

resulting in:

Although inventory data is correct (lower than the first test traces, but the problem is the same):

But the device name actually exists is this:

In str.getBytes (); plus encoding format like -> str.getBytes ( "UTF-8");

Like this ok.

See below:

However, there are problems, though like this change, but in fact, use the command query or distortion. The following example

When the query is, in turn, that the method used hexadecimal string turn Chinese hexStr2Str.

To get the name of the packet is "e68a80e69cafe983a8e997a8e99481"

Sample code modulation method: System.out.println ( "I'm getting it Chinese ......" + hexStr2Str ( "e68a80e69cafe983a8e997a8e99481"));

Original printing method is this:

After repeated attempts to find garbage in the project there are two,

1. If abcdef appear in lower case will be garbled,

2.new String encoding format will not increase garbled (time).

Thus improving the method of Chinese hexadecimal string transfer, as follows:

/**
	 * 16进制直接转换成为字符串(无需Unicode解码)
	 * @param hexStr  Byte字符串(Byte之间无分隔符
	 * @author xxs
	 * @return 对应的字符串
	 */
	public static String hexStr2Str(String hex) {
		String hexStr = "";
		String str = "0123456789ABCDEF"; //16进制能用到的所有字符 0-15
		for(int i=0;i<hex.length();i++){
			String s = hex.substring(i, i+1);
			if(s.equals("a")||s.equals("b")||s.equals("c")||s.equals("d")||s.equals("e")||s.equals("f")){
				s=s.toUpperCase().substring(0, 1);
			}
			hexStr+=s;
		}
		
		char[] hexs = hexStr.toCharArray();//toCharArray() 方法将字符串转换为字符数组。
		int length = (hexStr.length() / 2);//1个byte数值 -> 两个16进制字符
		byte[] bytes = new byte[length]; 
		int n;
		for (int i = 0; i < bytes.length; i++) {
			int position = i * 2;//两个16进制字符 -> 1个byte数值
			n = str.indexOf(hexs[position]) * 16;
			n += str.indexOf(hexs[position + 1]);
			// 保持二进制补码的一致性 因为byte类型字符是8bit的  而int为32bit 会自动补齐高位1  所以与上0xFF之后可以保持高位一致性 
			//当byte要转化为int的时候,高的24位必然会补1,这样,其二进制补码其实已经不一致了,&0xff可以将高的24位置为0,低8位保持原样,这样做的目的就是为了保证二进制数据的一致性。
			bytes[i] = (byte) (n & 0xff);
		}
		String name = "";
		try {
			name = new String(bytes,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return name;
	}

Print out something like this:

Completely solve the garbage problem.

 

 

 

 

 

 

Published 141 original articles · won praise 33 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43560721/article/details/102664784