16进制转中文,16进制转数字

二话不说 ,直接上代码

import java.math.BigInteger;

public class Util {
/**
 * 16进制转中文(GBK)
 * @param s
 * @return
 */
	public static String hexToStringGBK(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
				return "";
			}
		}
		try {
			s = new String(baKeyword, "GBK");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
			return "";
		}
		return s;
	}
	
	/**
	 * 16进制转中文(utf-8)
	 * @param s
	 * @return
	 */
	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "utf-8");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}
	
	/**
	 * 高字节在前,低字节在后(先转换)
	 * 例如 9D58B9FE82 首先转换成  82FEB9589D
	 * @param s
	 * @return
	 */
	public static String longToHexString (String s){
		Long c = Long.parseLong(s.substring(0, 8), 16);
		Long res = 0l;
		
		res |= (c & 0x000000ff) << 24;
		res |= (c & 0x0000ff00) << 8;
		res |= (c & 0x00ff0000) >> 8;
		res |= (int)((c & 0xff000000) >> 24);
		
		s = new BigInteger(Long.toHexString(res), 16).toString();
		
		return s;
	}
	
	
	public static void main(String[] args) {
		
	}
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xz475399320/article/details/46827843
文章标签: Java 16进制 中文
个人分类: 玩转java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭