Java 字串與byte之間的相互轉換
阿新 • • 發佈:2019-01-05
寫在前面
你是否跟我一樣,在一些需要加密的程式碼裡看見需要把字串轉換為位元組的場景,例如MD5加密。而你每次看到像我以前一樣,自動地略過(反正別人都寫好了,我用就是了)。如果是的話,不要再逃避了,下面跟我一起來了解一下字串與byte之間轉換的原理
正文
原理
我們都知道,在Java裡byte型別是佔用1個位元組,即8位的,而16進位制的字元佔用4位,所以每個byte可以用兩個字元來表示,反之亦然。舉個栗子
byte = 123
用二進位制表示:0111 1011
每4位用字元表示: 7 b
是的,原理就這麼簡單,接下來用程式碼實現:
- byte[] 轉16進位制字串
法1
思路:先把byte[] 轉換維char[],再把char[] 轉換為字串
public static String bytes2Hex(byte[] src) {
if (src == null || src.length <= 0) {
return null;
}
char[] res = new char[src.length * 2]; // 每個byte對應兩個字元
final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c' , 'd', 'e', 'f' };
for (int i = 0, j = 0; i < src.length; i++) {
res[j++] = hexDigits[src[i] >> 4 & 0x0f]; // 先存byte的高4位
res[j++] = hexDigits[src[i] & 0x0f]; // 再存byte的低4位
}
return new String(res);
}
法2
思路:先把byte轉換為int型別,再轉換為字串
public static String bytesToHex(byte[] src){
if (src == null || src.length <= 0) {
return null;
}
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < src.length; i++) {
// 之所以用byte和0xff相與,是因為int是32位,與0xff相與後就捨棄前面的24位,只保留後8位
String str = Integer.toHexString(src[i] & 0xff);
if (str.length() < 2) { // 不足兩位要補0
stringBuilder.append(0);
}
stringBuilder.append(str);
}
return stringBuilder.toString();
}
- 16進位制字串轉byte[]
思路:先把字串轉換為char[],再轉換為byte[]
public static byte[] hexToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] bytes = new byte[length];
String hexDigits = "0123456789abcdef";
for (int i = 0; i < length; i++) {
int pos = i * 2; // 兩個字元對應一個byte
int h = hexDigits.indexOf(hexChars[pos]) << 4; // 注1
int l = hexDigits.indexOf(hexChars[pos + 1]); // 注2
if(h == -1 || l == -1) { // 非16進位制字元
return null;
}
bytes[i] = (byte) (h | l);
}
return bytes;
}
注:注1得到xxxx0000,注2得到0000xxxx,相或就把兩個字元轉換為一個byte了。
- 再舉個栗子
md5加密
public static String getMd5ByFile(File file) {
String ret= null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) > 0) {
md.update(buffer, 0, len);
}
ret = bytes2Hex(md.digest()); // 把md5加密後的byte[]轉換為字串
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ret;
}
寫在最後
好了,應該懂了吧,其實並不難的。上面的是我個人的理解,難免有錯。若有錯,歡迎指正。
如果這篇文章對你有幫助的話,不妨點個頂唄~