java 16进制编码字符串与字符串转换方法总结

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
/**
 * 16进制编码数字工具类
*

Title: HexUtils

*

Description:

*@author yuhy *@date 2019年6月26日 */ public class HexUtils { /** * 16进制数字字符集 */ private static String hexString="0123456789ABCDEF"; /** * 转化字符串为十六进制编码 */ public static String toHexString(String s){ String str=""; for (int i=0;i>4)); sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); } return sb.toString(); } /** * 将16进制数字解码成字符串,适用于所有字符(包括中文) */ public static String decode(String bytes) { ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); //将每2位16进制整数组装成一个字节 for(int i=0;iTitle: ToHexString

*

Description:

*@param str *@return */ public static String ToHexString(String str) { String str1 = ""; try { byte[] b = str.getBytes("gbk"); int i = 0; int max = b.length; for (; i < max; i++) { str1 = str1 + Integer.toHexString(b[i] & 0xFF); } } catch (UnsupportedEncodingException e) { System.out.println("异常信息ToHexString" + e.getMessage()); } return str1; } /** * 16进制编码字符串转字节数组 *

Title: toByteArray

*

Description:

*@param hexString *@return */ public static byte[] toByteArray(String hexString) { hexString = hexString.toLowerCase(); final byte[] byteArray = new byte[hexString.length() / 2]; int k = 0; for (int i = 0; i < byteArray.length; i++) {// 因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先 byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); byteArray[i] = (byte) (high << 4 | low); k += 2; } return byteArray; } /** * 字节数组转16进制编码字符串 *

Title: toHexString

*

Description:

*@param byteArray *@return */ public static String toHexString(byte[] byteArray) { String str = null; if (byteArray != null && byteArray.length > 0) { StringBuffer stringBuffer = new StringBuffer(byteArray.length); for (byte byteChar : byteArray) { stringBuffer.append(String.format("%02X", byteChar)); } str = stringBuffer.toString(); } return str; } /** * 字节数组转字符串 *

Title: ByteArrayDecode

*

Description:

*@param bytes *@return */ public static String ByteArray2String(byte[] bytes) { String hexStr = toHexString(bytes); return decode(hexStr); } public static void main(String[] args) { String str = "你好asbc121f,smf,m,ewflwf健康的"; String sljzstr = encode(str); System.out.println("字符串转16进制数字____"+sljzstr); System.out.println("16进制数字转成字符串____"+decode(sljzstr)); System.out.println("字节数组转字符串:"+ByteArray2String(str.getBytes())); int a = 1<<4|2; System.out.println("a="+a); } }

 

你可能感兴趣的:(java 16进制编码字符串与字符串转换方法总结)