国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java實現(xiàn)的進制轉換工具類完整示例

Java實現(xiàn)的進制轉換工具類完整示例

2021-05-13 11:37黃寶康 Java教程

這篇文章主要介紹了Java實現(xiàn)的進制轉換工具類,結合完整實例形式分析了Java實現(xiàn)二進制、十六進制、字符串、數(shù)組等相關轉換操作技巧,需要的朋友可以參考下

本文實例講述了java實現(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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.nio.charset.charset;
/**
 * 十六進制(簡寫為hex或下標16)在數(shù)學中是一種逢16進1的進位制,一般用數(shù)字0到9和字母a到f表示(其中:a~f即10~15)。<br>
 * 例如十進制數(shù)57,在二進制寫作111001,在16進制寫作39。<br>
 * 像java,c這樣的語言為了區(qū)分十六進制和十進制數(shù)值,會在十六進制數(shù)的前面加上 0x,比如0x20是十進制的32,而不是十進制的20<br>
 *
 * 參考:https://my.oschina.net/xinxingegeya/blog/287476
 *
 * @author looly
 *
 */
public class hexkit {
  /**
   * 用于建立十六進制字符的輸出的小寫字符數(shù)組
   */
  private static final char[] digits_lower = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  /**
   * 用于建立十六進制字符的輸出的大寫字符數(shù)組
   */
  private static final char[] digits_upper = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  //---------------------------------------------------------------------------------------------------- encode
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符數(shù)組
   *
   * @param data byte[]
   * @return 十六進制char[]
   */
  public static char[] encodehex(byte[] data) {
    return encodehex(data, true);
  }
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符數(shù)組
   *
   * @param str 字符串
   * @param charset 編碼
   * @return 十六進制char[]
   */
  public static char[] encodehex(string str, charset charset) {
    return encodehex(strkit.getbytes(str, charset), true);
  }
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符數(shù)組
   *
   * @param data byte[]
   * @param tolowercase <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式
   * @return 十六進制char[]
   */
  public static char[] encodehex(byte[] data, boolean tolowercase) {
    return encodehex(data, tolowercase ? digits_lower : digits_upper);
  }
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符串
   *
   * @param data byte[]
   * @return 十六進制string
   */
  public static string encodehexstr(byte[] data) {
    return encodehexstr(data, true);
  }
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符串
   *
   * @param data byte[]
   * @param tolowercase <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式
   * @return 十六進制string
   */
  public static string encodehexstr(byte[] data, boolean tolowercase) {
    return encodehexstr(data, tolowercase ? digits_lower : digits_upper);
  }
  //---------------------------------------------------------------------------------------------------- decode
  /**
   * 將十六進制字符數(shù)組轉換為字符串
   *
   * @param hexstr 十六進制string
   * @param charset 編碼
   * @return 字符串
   */
  public static string decodehexstr(string hexstr, charset charset) {
    if(strkit.isempty(hexstr)){
      return hexstr;
    }
    return decodehexstr(hexstr.tochararray(), charset);
  }
  /**
   * 將十六進制字符數(shù)組轉換為字符串
   *
   * @param hexdata 十六進制char[]
   * @param charset 編碼
   * @return 字符串
   */
  public static string decodehexstr(char[] hexdata, charset charset) {
    return strkit.str(decodehex(hexdata), charset);
  }
  /**
   * 將十六進制字符數(shù)組轉換為字節(jié)數(shù)組
   *
   * @param hexdata 十六進制char[]
   * @return byte[]
   * @throws runtimeexception 如果源十六進制字符數(shù)組是一個奇怪的長度,將拋出運行時異常
   */
  public static byte[] decodehex(char[] hexdata) {
    int len = hexdata.length;
    if ((len & 0x01) != 0) {
      throw new runtimeexception("odd number of characters.");
    }
    byte[] out = new byte[len >> 1];
    // two characters form the hex value.
    for (int i = 0, j = 0; j < len; i++) {
      int f = todigit(hexdata[j], j) << 4;
      j++;
      f = f | todigit(hexdata[j], j);
      j++;
      out[i] = (byte) (f & 0xff);
    }
    return out;
  }
  //---------------------------------------------------------------------------------------- private method start
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符串
   *
   * @param data byte[]
   * @param todigits 用于控制輸出的char[]
   * @return 十六進制string
   */
  private static string encodehexstr(byte[] data, char[] todigits) {
    return new string(encodehex(data, todigits));
  }
  /**
   * 將字節(jié)數(shù)組轉換為十六進制字符數(shù)組
   *
   * @param data byte[]
   * @param todigits 用于控制輸出的char[]
   * @return 十六進制char[]
   */
  private static char[] encodehex(byte[] data, char[] todigits) {
    int l = data.length;
    char[] out = new char[l << 1];
    // two characters form the hex value.
    for (int i = 0, j = 0; i < l; i++) {
      out[j++] = todigits[(0xf0 & data[i]) >>> 4];
      out[j++] = todigits[0x0f & data[i]];
    }
    return out;
  }
  /**
   * 將十六進制字符轉換成一個整數(shù)
   *
   * @param ch 十六進制char
   * @param index 十六進制字符在字符數(shù)組中的位置
   * @return 一個整數(shù)
   * @throws runtimeexception 當ch不是一個合法的十六進制字符時,拋出運行時異常
   */
  private static int todigit(char ch, int index) {
    int digit = character.digit(ch, 16);
    if (digit == -1) {
      throw new runtimeexception("illegal hexadecimal character " + ch + " at index " + index);
    }
    return digit;
  }
  //---------------------------------------------------------------------------------------- private method end
  /**
   * 2進制轉16進制
   * @param bstring 2進制字符串
   * @return
   */
  public static string binary2hex(string bstring) {
    if (bstring == null || bstring.equals("") || bstring.length() % 8 != 0)
      return null;
    stringbuffer tmp = new stringbuffer();
    int itmp = 0;
    for (int i = 0; i < bstring.length(); i += 4) {
      itmp = 0;
      for (int j = 0; j < 4; j++) {
        itmp += integer.parseint(bstring.substring(i + j, i + j + 1)) << (4 - j - 1);
      }
      tmp.append(integer.tohexstring(itmp));
    }
    return tmp.tostring();
  }
  /**
   * 16進制轉2進制
   * @param hexstring
   * @return
   */
  public static string hex2binary(string hexstring) {
    if (hexstring == null || hexstring.length() % 2 != 0)
      return null;
    string bstring = "", tmp;
    for (int i = 0; i < hexstring.length(); i++) {
      tmp = "0000" + integer.tobinarystring(integer.parseint(hexstring.substring(i, i + 1), 16));
      bstring += tmp.substring(tmp.length() - 4);
    }
    return bstring;
  }
  /**
   * 將二進制轉換成16進制
   * @param buf
   * @return
   */
  public static string binary2hex(byte buf[]) {
    stringbuffer sb = new stringbuffer();
    for (int i = 0; i < buf.length; i++) {
      string hex = integer.tohexstring(buf[i] & 0xff);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      sb.append(hex.touppercase());
    }
    return sb.tostring();
  }
  /**
   * 將16進制轉換為二進制
   * @param hexstr
   * @return
   */
  public static byte[] hex2byte(string hexstr) {
    if (hexstr.length() < 1)
      return null;
    byte[] result = new byte[hexstr.length() / 2];
    for (int i = 0; i < hexstr.length() / 2; i++) {
      int high = integer.parseint(hexstr.substring(i * 2, i * 2 + 1), 16);
      int low = integer.parseint(hexstr.substring(i * 2 + 1, i * 2 + 2), 16);
      result[i] = (byte) (high * 16 + low);
    }
    return result;
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:https://blog.csdn.net/huangbaokang/article/details/75034021

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 免费看的av | 中文字幕精品一区二区精品绿巨人 | 日韩精品91爱爱 | 欧美黄色网| 久久中文精品 | 欧美乱大交xxxxx春色视频 | 亚洲综合中文 | 日韩在线观看 | 午夜成人免费视频 | 欧美精品v国产精品v日韩精品 | 狠狠综合久久av一区二区老牛 | 欧美日韩免费看 | 久久最新 | 国产精品久久久久永久免费观看 | 日本中文字幕在线观看 | 亚洲久草| 亚洲社区在线 | 日韩精品一区二区在线观看视频 | 欧美二区在线 | 国产成人一区二区三区在线观看 | 中文字幕亚洲欧美日韩在线不卡 | 日韩电影在线 | 在线色综合 | 久久国产精品无码网站 | 亚州成人 | 免费精品视频 | 亚洲电影免费 | 日韩精品在线观看免费 | 成人高清 | 欧美污污 | 欧美日本高清 | 久久午夜视频 | 亚洲男人在线 | 国产高清一区二区 | 中文字幕精品一区二区三区精品 | 搡女人真爽免费午夜网站 | 成人在线国产 | 在线观看国产中文字幕 | 亚洲精品影院 | 中文字幕av一区二区三区免费看 | 国产美女久久久 |