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

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

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

服務器之家 - 編程語言 - JAVA教程 - Java基本類型與byte數組之間相互轉換方法

Java基本類型與byte數組之間相互轉換方法

2020-06-06 15:03jingxian JAVA教程

下面小編就為大家帶來一篇Java基本類型與byte數組之間相互轉換方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Java基本類型與byte數組之間相互轉換,剛剛寫的

?
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cn.teaey.utils;
 
import java.nio.charset.Charset;
 
public class ByteUtil
{
  public static byte[] getBytes(short data)
  {
    byte[] bytes = new byte[2];
    bytes[0] = (byte) (data & 0xff);
    bytes[1] = (byte) ((data & 0xff00) >> 8);
    return bytes;
  }
 
 
  public static byte[] getBytes(char data)
  {
    byte[] bytes = new byte[2];
    bytes[0] = (byte) (data);
    bytes[1] = (byte) (data >> 8);
    return bytes;
  }
 
 
  public static byte[] getBytes(int data)
  {
    byte[] bytes = new byte[4];
    bytes[0] = (byte) (data & 0xff);
    bytes[1] = (byte) ((data & 0xff00) >> 8);
    bytes[2] = (byte) ((data & 0xff0000) >> 16);
    bytes[3] = (byte) ((data & 0xff000000) >> 24);
    return bytes;
  }
 
 
  public static byte[] getBytes(long data)
  {
    byte[] bytes = new byte[8];
    bytes[0] = (byte) (data & 0xff);
    bytes[1] = (byte) ((data >> 8) & 0xff);
    bytes[2] = (byte) ((data >> 16) & 0xff);
    bytes[3] = (byte) ((data >> 24) & 0xff);
    bytes[4] = (byte) ((data >> 32) & 0xff);
    bytes[5] = (byte) ((data >> 40) & 0xff);
    bytes[6] = (byte) ((data >> 48) & 0xff);
    bytes[7] = (byte) ((data >> 56) & 0xff);
    return bytes;
  }
 
 
  public static byte[] getBytes(float data)
  {
    int intBits = Float.floatToIntBits(data);
    return getBytes(intBits);
  }
 
 
  public static byte[] getBytes(double data)
  {
    long intBits = Double.doubleToLongBits(data);
    return getBytes(intBits);
  }
 
 
  public static byte[] getBytes(String data, String charsetName)
  {
    Charset charset = Charset.forName(charsetName);
    return data.getBytes(charset);
  }
 
 
  public static byte[] getBytes(String data)
  {
    return getBytes(data, "GBK");
  }
 
 
  
  public static short getShort(byte[] bytes)
  {
    return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
  }
 
 
  public static char getChar(byte[] bytes)
  {
    return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
  }
 
 
  public static int getInt(byte[] bytes)
  {
    return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
  }
  
  public static long getLong(byte[] bytes)
  {
    return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))
     | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
  }
 
 
  public static float getFloat(byte[] bytes)
  {
    return Float.intBitsToFloat(getInt(bytes));
  }
 
 
  public static double getDouble(byte[] bytes)
  {
    long l = getLong(bytes);
    System.out.println(l);
    return Double.longBitsToDouble(l);
  }
 
 
  public static String getString(byte[] bytes, String charsetName)
  {
    return new String(bytes, Charset.forName(charsetName));
  }
 
 
  public static String getString(byte[] bytes)
  {
    return getString(bytes, "GBK");
  }
 
 
  
  public static void main(String[] args)
  {
    short s = 122;
    int i = 122;
    long l = 1222222;
 
 
    char c = 'a';
 
 
    float f = 122.22f;
    double d = 122.22;
 
 
    String string = "我是好孩子";
    System.out.println(s);
    System.out.println(i);
    System.out.println(l);
    System.out.println(c);
    System.out.println(f);
    System.out.println(d);
    System.out.println(string);
 
 
    System.out.println("**************");
 
 
    System.out.println(getShort(getBytes(s)));
    System.out.println(getInt(getBytes(i)));
    System.out.println(getLong(getBytes(l)));
    System.out.println(getChar(getBytes(c)));
    System.out.println(getFloat(getBytes(f)));
    System.out.println(getDouble(getBytes(d)));
    System.out.println(getString(getBytes(string)));
  }
 
}

以上這篇Java基本類型與byte數組之間相互轉換方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91视频完整版 | 美女脱了内裤张开腿亲吻男生 | 四虎私人影院 | 日日爱爱 | 国产亚洲欧美一区二区三区 | 亚洲精品国产成人 | 成人黄色免费网站 | 成人免费一区二区三区在线观看 | 国产福利专区精品视频 | 日本人成大片在线 | 精品午夜寂寞影院在线观看 | 亚洲福利在线观看 | 日本护士撒尿xxxx18 | 偷拍自拍校园春色 | 欧美在线播放成人免费 | 日韩中文字幕视频在线观看 | 调教禽兽 | 国偷盗摄自产福利一区在线 | 99视频全部看免费观 | 情侣奴伺候女王第2部分小说 | 欧美一区二区三区gg高清影视 | 国产精品久久国产精品99盘 | 青草视频在线观看视频 | 91久久国产青草亚洲 | 亚洲 欧美 中文字幕 在线 | 亚洲成人国产 | 精品国产免费久久久久久婷婷 | 黄a在线观看 | 免看一级a一片成人123 | tolove第一季动画在线看 | 91高清国产经典在线观看 | 女人麻豆国产香蕉久久精品 | 韩国黄色片网站 | 扒开大腿狠狠挺进视频 | 免费看h片的网站 | 国产精品国产三级国产专区不 | a片毛片在线免费看 | 91看片淫黄大片.在线天堂 | 高h全肉动漫在线观看免费 高h辣h双处全是肉军婚 | 99精品国产美女福到在线不卡 | 日本videos有奶水的hd |