关于字符串进行十六进制转换的集中方法

package com.test;


import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Formatter;


import javax.net.ssl.SSLContext;


public class TestBoolean {


private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F' };


/**
* 位运算进行16进制转换

* @param tm
* @return
*/
public static String getStr1(String tm) {


String str = null;
try {
byte[] bt = tm.getBytes("utf-8");// 把字符串转换成字节数组
System.out.println("方法一:bt字节数为:" + Arrays.toString(bt));
int length = bt.length;
char[] cha = new char[length * 2];
int m = 0;
for (byte c : bt) {
cha[m++] = HEX_CHAR[c >>> 4 & 0xf];
cha[m++] = HEX_CHAR[c & 0xf];
}
str = new String(cha);
} catch (UnsupportedEncodingException e) {


e.printStackTrace();
}
System.out.println("strs是-----" + str);
return str;


}


/**
* 使用除与区域进行转换

* @param tm
* @return
*/
public static String getStr2(String tm) {


String str = null;
try {
byte[] bt = tm.getBytes("utf-8");// 把字符串转换成字节数组
System.out.println("方法二:bt字节数为:" + Arrays.toString(bt));
int length = bt.length;
char[] cha = new char[length * 2];
int m = 0;
int n = 0;
for (byte c : bt) {


if (c < 0) {
n = 256 + c;
} else {
n = c;
}
cha[m++] = HEX_CHAR[n / 16];
cha[m++] = HEX_CHAR[n % 16];


}
str = new String(cha);
} catch (UnsupportedEncodingException e) {


e.printStackTrace();
}
System.out.println("strs是-----" + str);
return str;


}


/**
* 格式化字符串位16进制

* @param tm
* @return
*/
public static String getStr3(String tm) {


StringBuilder s = new StringBuilder();
try {
byte[] bt = tm.getBytes("utf-8");// 把字符串转换成字节数组
System.out.println("方法三:bt字节数为:" + Arrays.toString(bt));


for (byte c : bt) {
Integer i = new Integer(c & 0xff);// 把字节类型的c转换成int类型,
int类型在计算机中占有4个字节32位而字节类型的c只占了1个字节8位,具有一定的随机性,所以要把高24位规避掉

Formatter formatter = new Formatter();
String str = formatter.format("%02x", i).toString();// 把i格式化城16进制的字符串
// ;%02X:以十六进制输出,2为指定的输出字段的宽度.如果位数小于2,则左端补0;

s.append(str.toUpperCase());
}


} catch (UnsupportedEncodingException e) {


e.printStackTrace();
}
System.out.println("strs是-----" + s.toString());
return s.toString();


}


/**
* 格式化字符串位16进制

* @param tm
* @return
*/
public static String getStr4(String tm) {


StringBuilder s = new StringBuilder();
try {
byte[] bt = tm.getBytes("utf-8");// 把字符串转换成字节数组
System.out.println("方法四:bt字节数为:" + Arrays.toString(bt));


for (byte c : bt) {


Formatter formatter = new Formatter();
String str = formatter.format("%02x", c).toString();// 把i格式化城16进制的字符串
// ;%02X:以十六进制输出,2为指定的输出字段的宽度.如果位数小于2,则左端补0;
s.append(str.toUpperCase());
}


} catch (UnsupportedEncodingException e) {


e.printStackTrace();
}
System.out.println("strs是-----" + s.toString());
return s.toString();


}


/**
* 将16进制字符串转换为byte[]

* @param str
* @return
*/
public static byte[] strToBytes(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;
}


public static void main(String[] args) {
String tm = "中华人民共和国";
getStr1(tm);
getStr2(tm);
getStr3(tm);
getStr4(tm);
}


}

输出结果:





阅读更多
想对作者说点什么? 我来说一句

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