一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 淺談二進制、十進制、十六進制、字符串之間的相互轉換

淺談二進制、十進制、十六進制、字符串之間的相互轉換

2020-05-15 10:53jingxian JAVA教程

下面小編就為大家帶來一篇淺談二進制、十進制、十六進制、字符串之間的相互轉換。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧

1. 字節轉10進制

直接使用(int)類型轉換。

?
1
2
3
4
5
6
7
/*
   * 字節轉10進制
   */
  public static int byte2Int(byte b){
    int r = (int) b;
    return r;
  }

2. 10進制轉字節

直接使用(byte)類型轉換。

?
1
2
3
4
5
6
7
/*
   * 10進制轉字節
   */
  public static byte int2Byte(int i){
    byte r = (byte) i;
    return r;
  }

3. 字節數組轉16進制字符串

對每一個字節,先和0xFF做與運算,然后使用Integer.toHexString()函數,如果結果只有1位,需要在前面加0。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
   * 字節數組轉16進制字符串
   */
  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進制字符串轉字節數組

這個比較復雜,每一個16進制字符是4bit,一個字節是8bit,所以兩個16進制字符轉換成1個字節,對于第1個字符,轉換成byte以后左移4位,然后和第2個字符的byte做或運算,這樣就把兩個字符轉換為1個字節。

?
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
/*
   * 字符轉換為字節
   */
  private static byte charToByte(char c) {
    return (byte) "0123456789ABCDEF".indexOf(c);
   }
  
  /*
   * 16進制字符串轉字節數組
   */
  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. 字節數組轉字符串

直接使用new String()。

?
1
2
3
4
5
6
7
/*
   * 字節數組轉字符串
   */
  public static String bytes2String(byte[] b) throws Exception {
    String r = new String (b,"UTF-8");   
    return r;
  }

6. 字符串轉字節數組

直接使用getBytes()。

?
1
2
3
4
5
6
7
/*
   * 字符串轉字節數組
   */
  public static byte[] string2Bytes(String s){
    byte[] r = s.getBytes();
    return r;
  }

7. 16進制字符串轉字符串

先轉換成byte[],再轉換成字符串。

?
1
2
3
4
5
6
7
/*
   * 16進制字符串轉字符串
   */
  public static String hex2String(String hex) throws Exception{
    String r = bytes2String(hexString2Bytes(hex));   
    return r;
  }

8. 字符串轉16進制字符串

先轉換為byte[],再轉換為16進制字符串。

?
1
2
3
4
5
6
7
/*
   * 字符串轉16進制字符串
   */
  public static String string2HexString(String s) throws Exception{
    String r = bytes2HexString(string2Bytes(s));   
    return r;
  }

main函數:

?
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.字節轉10進制:" + byte2Int(b1));
    
    int i = 89;
    System.out.println("2.10進制轉字節:" + int2Byte(i));
    
    byte[] b2 = new byte[]{(byte)0xFF, (byte)0x5F, (byte)0x6, (byte)0x5A};
    System.out.println("3.字節數組轉16進制字符串:" + bytes2HexString(b2));
    
    String s1 = new String("1DA47C");
    System.out.println("4.16進制字符串轉字節數組:" + Arrays.toString(hexString2Bytes(s1)));
    
    System.out.println("5.字節數組轉字符串:" + bytes2String(b2));
    
    System.out.println("6.字符串轉字節數組:" + Arrays.toString(string2Bytes(s1)));
    
    System.out.println("7.16進制字符串轉字符串:" + hex2String(s1));
    
    String s2 = new String("Hello!");
    System.out.println("8.字符串轉16進制字符串:" + string2HexString(s2));
  }

運行結果:

?
1
2
3
4
5
6
7
8
1.字節轉10進制:45
2.10進制轉字節:89
3.字節數組轉16進制字符串:FF5F065A
4.16進制字符串轉字節數組:[29, -92, 124]
5.字節數組轉字符串:?_Z
6.字符串轉字節數組:[49, 68, 65, 52, 55, 67]
7.16進制字符串轉字符串:?|
8.字符串轉16進制字符串:48656C6C6F21

以上這篇淺談二進制、十進制、十六進制、字符串之間的相互轉換就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品高清一区二区三区不卡 | 色综合久久中文字幕网 | 国内自拍网红在综合图区 | 91李宗精品72集在线观看 | 美女扒下内裤让男人桶的图片 | 女人张开腿让男人桶视频免费大全 | 国产一级毛片潘金莲的奶头 | 狠狠色狠狠色综合曰曰 | 大团圆6全文在线阅读 | 2019午夜福合集高清完整版 | 国产良心大作白丝精厕 | 五月天狠狠 | 无码人妻丰满熟妇啪啪网不卡 | 亚洲国产综合久久精品 | 国产一区二区三区久久精品小说 | 免费网址在线观看入口推荐 | 日韩欧美中文字幕一区二区三区 | 国色天香社区在线视频免费观看 | 精品视频在线观看免费 | 99色在线观看 | 国产精品久久久久a影院 | 99热这里只有精品在线播放 | 精品一区二区三区免费站 | 亚洲区在线播放 | 91久久国产| 国产在线拍 | 91欧美秘密入口 | 亚洲成年www | 98pao强力打造高清免费 | 我要看免费毛片 | 九九99九九精彩网站 | japanesepooping脱粪 | 国产片自拍 | 男生操女生漫画 | 亚洲国产精品无码中文字满 | 深夜视频免费看 | 男生操女生动态图 | 国内视频一区二区 | 国产高清不卡视频在线播放 | 四虎成人免费视频 | 免费成人在线观看视频 |