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

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

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

服務器之家 - 編程語言 - JAVA教程 - java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法

java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法

2020-06-21 15:19jingxian JAVA教程

下面小編就為大家?guī)硪黄猨ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

實例如下:

?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class DataTypeChangeHelper {
  /**
   * 將一個單字節(jié)的byte轉(zhuǎn)換成32位的int
   *
   * @param b
   *      byte
   * @return convert result
   */
  public static int unsignedByteToInt(byte b) {
    return (int) b & 0xFF;
  }
 
  /**
   * 將一個單字節(jié)的Byte轉(zhuǎn)換成十六進制的數(shù)
   *
   * @param b
   *      byte
   * @return convert result
   */
  public static String byteToHex(byte b) {
    int i = b & 0xFF;
    return Integer.toHexString(i);
  }
 
  /**
   * 將一個4byte的數(shù)組轉(zhuǎn)換成32位的int
   *
   * @param buf
   *      bytes buffer
   * @param byte[]中開始轉(zhuǎn)換的位置
   * @return convert result
   */
  public static long unsigned4BytesToInt(byte[] buf, int pos) {
    int firstByte = 0;
    int secondByte = 0;
    int thirdByte = 0;
    int fourthByte = 0;
    int index = pos;
    firstByte = (0x000000FF & ((int) buf[index]));
    secondByte = (0x000000FF & ((int) buf[index + 1]));
    thirdByte = (0x000000FF & ((int) buf[index + 2]));
    fourthByte = (0x000000FF & ((int) buf[index + 3]));
    index = index + 4;
    return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
  }
 
  /**
   * 將16位的short轉(zhuǎn)換成byte數(shù)組
   *
   * @param s
   *      short
   * @return byte[] 長度為2
   * */
  public static byte[] shortToByteArray(short s) {
    byte[] targets = new byte[2];
    for (int i = 0; i < 2; i++) {
      int offset = (targets.length - 1 - i) * 8;
      targets[i] = (byte) ((s >>> offset) & 0xff);
    }
    return targets;
  }
 
  /**
   * 將32位整數(shù)轉(zhuǎn)換成長度為4的byte數(shù)組
   *
   * @param s
   *      int
   * @return byte[]
   * */
  public static byte[] intToByteArray(int s) {
    byte[] targets = new byte[2];
    for (int i = 0; i < 4; i++) {
      int offset = (targets.length - 1 - i) * 8;
      targets[i] = (byte) ((s >>> offset) & 0xff);
    }
    return targets;
  }
 
  /**
   * long to byte[]
   *
   * @param s
   *      long
   * @return byte[]
   * */
  public static byte[] longToByteArray(long s) {
    byte[] targets = new byte[2];
    for (int i = 0; i < 8; i++) {
      int offset = (targets.length - 1 - i) * 8;
      targets[i] = (byte) ((s >>> offset) & 0xff);
    }
    return targets;
  }
 
  /**32位int轉(zhuǎn)byte[]*/
  public static byte[] int2byte(int res) {
    byte[] targets = new byte[4];
    targets[0] = (byte) (res & 0xff);// 最低位
    targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
    targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
    targets[3] = (byte) (res >>> 24);// 最高位,無符號右移。
    return targets;
  }
 
  /**
   * 將長度為2的byte數(shù)組轉(zhuǎn)換為16位int
   *
   * @param res
   *      byte[]
   * @return int
   * */
  public static int byte2int(byte[] res) {
    // res = InversionByte(res);
    // 一個byte數(shù)據(jù)左移24位變成0x??000000,再右移8位變成0x00??0000
    int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
    return targets;
  }
}

以上就是小編為大家?guī)淼膉ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法全部內(nèi)容了,希望大家多多支持服務器之家~

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久re热在线视频精99 | 99在线观看视频免费精品9 | 精品欧美一区二区在线观看欧美熟 | 非洲黑人又大粗gay 非洲黑人bbwbbwbbw | 色欲麻豆国产福利精品 | 果冻传媒林予曦图片 | 国产卡一卡二卡三卡四 | 草草视频免费观看 | 日韩一区二区三区四区五区 | 国产亚洲欧美成人久久片 | 免费看隐私美女 | 草草视频免费在线观看 | 精精国产www视频在线观看免费 | 9999网站| 人人澡 人人澡碰人人看软件 | 故意短裙公车被强好爽在线播放 | 国产欧美日韩专区毛茸茸 | 4hc44四虎www在线影院男同 | 亚洲天堂2016 | 好湿好滑好硬好爽好深视频 | 91在线精品老司机免费播放 | 69日本xxⅹxxxxx19 | 国产精品久久久久久福利 | 成人在线视频国产 | 精品无人区麻豆乱码无限制 | 91搞搞| 国产91无毒不卡在线观看 | 成年人在线观看视频 | 婷婷在线观看香蕉五月天 | 国产精品边做边接电话在线观看 | 亚洲国产精品自产在线播放 | 日本久久啪啪婷婷激情五月 | 无码国产成人午夜在线观看不卡 | 免费观看一区二区 | 久久精品熟女亚洲AV国产 | 香港日本三级亚洲三级 | 国产91第一页 | 国产精品久久现线拍久青草 | 国产精品福利在线观看秒播 | 超级碰碰青草免费视频92 | 久久精品国产亚洲AV热无遮挡 |