GBK编码转换及Md5算法工具
生活随笔
收集整理的這篇文章主要介紹了
GBK编码转换及Md5算法工具
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import java.io.UnsupportedEncodingException;/*** @author lmg* @version v1.0* @Description* @date 2020-12-16 17:08*/
public class Utils {/*** 把16進(jìn)制的轉(zhuǎn)字符串* @param s* @return*/public static String hexToStringGBK(String s){String tmp = null;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 {tmp = new String(baKeyword, "GBK");// UTF-16le:Not} catch (Exception e1) {e1.printStackTrace();}return tmp;}/*** 將字符串轉(zhuǎn)為指定編碼的16進(jìn)制** @param str* @return*/public static String strToHexStr(String str) {String hexString = "0123456789ABCDEF";//根據(jù)編碼獲取字節(jié)數(shù)組byte[] bytes = new byte[0];try {bytes = str.getBytes("GBK");} catch (UnsupportedEncodingException e) {e.printStackTrace();}StringBuilder sb = new StringBuilder(bytes.length * 2);//將字節(jié)數(shù)組中每個(gè)字節(jié)拆解成2位16進(jìn)制整數(shù)for (int i = 0; i < bytes.length; i++) {sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));}return sb.toString();}/*** 漢字轉(zhuǎn)換成 gb2312 的ascii碼** @param str* @return* @throws Exception*/public static String wordToAsciiGB2312(String str){try {byte[] temp = str.getBytes("utf-8"); //這里寫原編碼的方式byte[] bArray = new String(temp, "utf-8").getBytes("gbk");//這里轉(zhuǎn)換后的編碼方式StringBuffer sb = new StringBuffer(bArray.length);for (int i = 0; i < bArray.length; i++) {String sTemp = Integer.toHexString(0xFF & bArray[i]);if (sTemp.length() < 2)sb.append(0);sb.append(sTemp.toUpperCase());}return sb.toString();}catch (Exception e){e.printStackTrace();return null;}}/*** byte[]轉(zhuǎn)十六進(jìn)制字符串* @param bytes* @return*/public static String bytesToHex(byte[] bytes) {StringBuilder buf = new StringBuilder(bytes.length * 2);for(byte b : bytes) { // 使用String的format方法進(jìn)行轉(zhuǎn)換buf.append(String.format("%02X", new Integer(b & 0xff)));}return buf.toString();}/*** 將16進(jìn)制字符串轉(zhuǎn)換為byte[]** @param str* @return*/public static byte[] toBytes(String str) {if(str == null || str.trim().equals("")) {return new byte[0];}byte[] bytes = new byte[str.length() / 2];for(int i = 0; i < str.length() / 2; i++) {String subStr = str.substring(i * 2, i * 2 + 2);bytes[i] = (byte) Integer.parseInt(subStr, 16);}return bytes;}/*** 16進(jìn)制數(shù)據(jù)取反* @return*/public static String reverse(String data){byte[] abts = toBytes(data);String sTemp="";for (int i = 0; i < abts.length; i++) {sTemp += String.format("%02X",0xFF &~ abts[i]);}return sTemp.toUpperCase();}/*** Md5數(shù)據(jù)加密* @return*/public final static String toMD5(String s) {char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F'};try {byte[] strTemp = s.getBytes("UTF-8");MessageDigest mdTemp = MessageDigest.getInstance("MD5");mdTemp.update(strTemp);byte[] md = mdTemp.digest();int j = md.length;char str[] = new char[j * 2];int k = 0;for (int i = 0; i < j; i++) {byte byte0 = md[i];str[k++] = hexDigits[byte0 >>> 4 & 0xF];str[k++] = hexDigits[byte0 & 0xF];}return new String(str);} catch (Exception e) {e.printStackTrace();return null;}}}
總結(jié)
以上是生活随笔為你收集整理的GBK编码转换及Md5算法工具的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Axure 制作验证码交互
- 下一篇: 新增书籍以及上下架