1. 字節(jié)轉(zhuǎn)10進(jìn)制
直接使用(int)類型轉(zhuǎn)換。
1
2
3
4
5
6
7
|
/* * 字節(jié)轉(zhuǎn)10進(jìn)制 */ public static int byte2Int( byte b){ int r = ( int ) b; return r; } |
2. 10進(jìn)制轉(zhuǎn)字節(jié)
直接使用(byte)類型轉(zhuǎn)換。
1
2
3
4
5
6
7
|
/* * 10進(jìn)制轉(zhuǎn)字節(jié) */ public static byte int2Byte( int i){ byte r = ( byte ) i; return r; } |
3. 字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串
對(duì)每一個(gè)字節(jié),先和0xFF做與運(yùn)算,然后使用Integer.toHexString()函數(shù),如果結(jié)果只有1位,需要在前面加0。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/* * 字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串 */ public static String bytes2HexString( byte [] b) { String r = "" ; for ( int i = 0 ; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF ); if (hex.length() == 1 ) { hex = '0' + hex; } r += hex.toUpperCase(); } return r; } |
4. 16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組
這個(gè)比較復(fù)雜,每一個(gè)16進(jìn)制字符是4bit,一個(gè)字節(jié)是8bit,所以兩個(gè)16進(jìn)制字符轉(zhuǎn)換成1個(gè)字節(jié),對(duì)于第1個(gè)字符,轉(zhuǎn)換成byte以后左移4位,然后和第2個(gè)字符的byte做或運(yùn)算,這樣就把兩個(gè)字符轉(zhuǎn)換為1個(gè)字節(jié)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/* * 字符轉(zhuǎn)換為字節(jié) */ private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /* * 16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組 */ public static byte [] hexString2Bytes(String hex) { if ((hex == null ) || (hex.equals( "" ))){ return null ; } else if (hex.length()% 2 != 0 ){ return null ; } else { hex = hex.toUpperCase(); int len = hex.length()/ 2 ; byte [] b = new byte [len]; char [] hc = hex.toCharArray(); for ( int i= 0 ; i<len; i++){ int p= 2 *i; b[i] = ( byte ) (charToByte(hc[p]) << 4 | charToByte(hc[p+ 1 ])); } return b; } } |
5. 字節(jié)數(shù)組轉(zhuǎn)字符串
直接使用new String()。
1
2
3
4
5
6
7
|
/* * 字節(jié)數(shù)組轉(zhuǎn)字符串 */ public static String bytes2String(byte[] b) throws Exception { String r = new String (b,"UTF-8"); return r; } |
6. 字符串轉(zhuǎn)字節(jié)數(shù)組
直接使用getBytes()。
1
2
3
4
5
6
7
|
/* * 字符串轉(zhuǎn)字節(jié)數(shù)組 */ public static byte [] string2Bytes(String s){ byte [] r = s.getBytes(); return r; } |
7. 16進(jìn)制字符串轉(zhuǎn)字符串
先轉(zhuǎn)換成byte[],再轉(zhuǎn)換成字符串。
1
2
3
4
5
6
7
|
/* * 16進(jìn)制字符串轉(zhuǎn)字符串 */ public static String hex2String(String hex) throws Exception{ String r = bytes2String(hexString2Bytes(hex)); return r; } |
8. 字符串轉(zhuǎn)16進(jìn)制字符串
先轉(zhuǎn)換為byte[],再轉(zhuǎn)換為16進(jìn)制字符串。
1
2
3
4
5
6
7
|
/* * 字符串轉(zhuǎn)16進(jìn)制字符串 */ public static String string2HexString(String s) throws Exception{ String r = bytes2HexString(string2Bytes(s)); return r; } |
main函數(shù):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public static void main(String[] args) throws Exception{ byte b1 = ( byte ) 45 ; System.out.println( "1.字節(jié)轉(zhuǎn)10進(jìn)制:" + byte2Int(b1)); int i = 89 ; System.out.println( "2.10進(jìn)制轉(zhuǎn)字節(jié):" + int2Byte(i)); byte [] b2 = new byte []{( byte ) 0xFF , ( byte ) 0x5F , ( byte ) 0x6 , ( byte ) 0x5A }; System.out.println( "3.字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串:" + bytes2HexString(b2)); String s1 = new String( "1DA47C" ); System.out.println( "4.16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組:" + Arrays.toString(hexString2Bytes(s1))); System.out.println( "5.字節(jié)數(shù)組轉(zhuǎn)字符串:" + bytes2String(b2)); System.out.println( "6.字符串轉(zhuǎn)字節(jié)數(shù)組:" + Arrays.toString(string2Bytes(s1))); System.out.println( "7.16進(jìn)制字符串轉(zhuǎn)字符串:" + hex2String(s1)); String s2 = new String( "Hello!" ); System.out.println( "8.字符串轉(zhuǎn)16進(jìn)制字符串:" + string2HexString(s2)); } |
運(yùn)行結(jié)果:
1
2
3
4
5
6
7
8
|
1 .字節(jié)轉(zhuǎn) 10 進(jìn)制: 45 2.10 進(jìn)制轉(zhuǎn)字節(jié): 89 3 .字節(jié)數(shù)組轉(zhuǎn) 16 進(jìn)制字符串:FF5F065A 4.16 進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組:[ 29 , - 92 , 124 ] 5 .字節(jié)數(shù)組轉(zhuǎn)字符串:?_Z 6 .字符串轉(zhuǎn)字節(jié)數(shù)組:[ 49 , 68 , 65 , 52 , 55 , 67 ] 7.16 進(jìn)制字符串轉(zhuǎn)字符串:?| 8 .字符串轉(zhuǎn) 16 進(jìn)制字符串:48656C6C6F21 |
以上這篇淺談二進(jìn)制、十進(jìn)制、十六進(jìn)制、字符串之間的相互轉(zhuǎn)換就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。