16进制转字符串_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 16进制转字符串

16进制转字符串

 2015/2/9 18:48:55  chenliang1234576  程序员俱乐部  我要评论(0)
  • 摘要:packagecn.luhui.yuerle.utils;/***@ClassName:HexDecode*@Description:16进制转码*@authorChenliang*@date2014-12-17下午3:09:31**/publicclassHexDecode{/***将指定字符串src,以每两个字符分割转换为16进制形式如:"2B44EFD9"-->byte[]{0x2B,0x44,0xEF,*0xD9
  • 标签:字符串

package cn.luhui.yuerle.utils;

/**
?* @ClassName: HexDecode
?* @Description: 16进制转码
?* @author Chenliang
?* @date 2014-12-17 下午3:09:31
?*
?*/
public class HexDecode {

?/**
? * 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,
? * 0xD9}
? *
? * @param src
? *??????????? String
? * @return byte[]
? */
?public static byte[] HexString2Bytes(String src) {
??src = src.replace(" ", "");
??int len = src.length() / 2;
??byte[] ret = new byte[len];
??byte[] tmp = src.getBytes();
??for (int i = 0; i < len; i++) {
???ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
??}
??return ret;
?}

?/**
? * 将指定byte数组以16进制的形式打印到控制台
? *
? * @param hint
? *??????????? String
? * @param b
? *??????????? byte[]
? * @return void
? */
?public static void printHexString(String hint, byte[] b) {
??System.out.print(hint);
??for (int i = 0; i < b.length; i++) {
???String hex = Integer.toHexString(b[i] & 0xFF);
???if (hex.length() == 1) {
????hex = '0' + hex;
???}
???System.out.print(hex.toUpperCase() + " ");
??}
??System.out.println("");
?}

?/**
? *
? * @param b
? *??????????? byte[]
? * @return String
? */
?public static String Bytes2HexString(byte[] b) {
??String ret = "";
??for (int i = 0; i < b.length; i++) {
???String hex = Integer.toHexString(b[i] & 0xFF);
???if (hex.length() == 1) {
????hex = '0' + hex;
???}
???ret += hex.toUpperCase();
??}
??return ret;
?}

?/**
? * 将两个ASCII字符合成一个字节; 如:"EF"--> 0xEF
? *
? * @param src0
? *??????????? byte
? * @param src1
? *??????????? byte
? * @return byte
? */
?public static byte uniteBytes(byte src0, byte src1) {
??byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
????.byteValue();
??_b0 = (byte) (_b0 << 4);
??byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
????.byteValue();
??byte ret = (byte) (_b0 ^ _b1);
??return ret;
?}
?

???
???
??? /**
???? * java字节码转字符串
???? * @param b
???? * @return
???? */
??? public static String byte2hex(byte[] b) { //一个字节的数,

??????? // 转成16进制字符串

??????? String hs = "";
??????? String tmp = "";
??????? for (int n = 0; n < b.length; n++) {
??????????? //整数转成十六进制表示

??????????? tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
??????????? if (tmp.length() == 1) {
??????????????? hs = hs + "0" + tmp;
??????????? } else {
??????????????? hs = hs + tmp;
??????????? }
??????? }
??????? tmp = null;
??????? return hs.toUpperCase(); //转成大写

??? }

??? /**
???? * 字符串转java字节码
???? * @param b
???? * @return
???? */
??? public static byte[] hex2byte(byte[] b) {
??????? if ((b.length % 2) != 0) {
??????????? throw new IllegalArgumentException("长度不是偶数");
??????? }
??????? byte[] b2 = new byte[b.length / 2];
??????? for (int n = 0; n < b.length; n += 2) {
??????????? String item = new String(b, n, 2);
??????????? // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节

??????????? b2[n / 2] = (byte) Integer.parseInt(item, 16);
??????? }
??????? b = null;
??????? return b2;
??? }
?
?

?// 十进制转化为十六进制
?public static String to16Value(int a)
?{
??return ?Integer.toHexString(a);
?}
?// 十六进制转化为十进制
?public static int to10Value(String aa)
?{
??return Integer.parseInt(aa,16);
?}
?
? /**
???? * 字符串转换成十六进制值
???? * @param bin String 我们看到的要转换成十六进制的字符串
???? * @return
???? */
??? public static String bin2hex(String bin) {
??????? char[] digital = "0123456789ABCDEF".toCharArray();
??????? StringBuffer sb = new StringBuffer("");
??????? byte[] bs = bin.getBytes();
??????? int bit;
??????? for (int i = 0; i < bs.length; i++) {
??????????? bit = (bs[i] & 0x0f0) >> 4;
??????????? sb.append(digital[bit]);
??????????? bit = bs[i] & 0x0f;
??????????? sb.append(digital[bit]);
??????? }
??????? return sb.toString();
??? }
??? /**
???? * 十六进制转换字符串
???? * @param hex String 十六进制
???? * @return String 转换后的字符串
???? */
??? public static String hex2bin(String hex) {
??????? String digital = "0123456789ABCDEF";
??????? char[] hex2char = hex.toCharArray();
??????? byte[] bytes = new byte[hex.length() / 2];
??????? int temp;
??????? for (int i = 0; i < bytes.length; i++) {
??????????? temp = digital.indexOf(hex2char[2 * i]) * 16;
??????????? temp += digital.indexOf(hex2char[2 * i + 1]);
??????????? bytes[i] = (byte) (temp & 0xff);
??????? }
??????? return new String(bytes);
??? }

}

发表评论
用户名: 匿名