转换咖啡

来一杯转换咖啡?

转换咖啡

进制转换

在进制转换界面实现了二、四、八、十、十六、三十二进制之间的相互转换。

在对应进制位置输入内容并点击后方按钮,即可转换为其余五种进制

编码转换

在编码转换界面,实现了Unicode,UTF-8,ASCII,Base64,URL的编码和解码。

编码:于第一个文本框内输入想要编码的内容,点击进行编码即可同时输出五种编码形式。

解码:于第一个文本框内输入想要解码的内容,点击进行解码即可解码,若是可以解码,则会在对于文本框内显示解码内容,反之文本框显示解码失败。

注意:

如果程序无法运行,大概率为使用者未正确安装和配置Java环境,请参考https://jingyan.baidu.com/article/f96699bb163475894e3c1be4.html进行配置,JDK安装包百度云版,提取码为x9fw

下载地址

https://github.com/Ku3n/SwitchCoffee

代码如下

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Base64;

public class Main {
static JFrame jfrm = new JFrame("转换咖啡");
static Container ct = jfrm.getContentPane();
static Image im = Toolkit.getDefaultToolkit().getImage("/icon.jpg");

static JLabel writer1 = new JLabel("made by ku3n|李香兰");
static JLabel email1 = new JLabel("ku3n@qq.com");
static JLabel writer2 = new JLabel("made by ku3n|李香兰");
static JLabel email2 = new JLabel("ku3n@qq.com");

static JLabel lab_2 = new JLabel("二进制:");
static JLabel lab_4 = new JLabel("四进制:");
static JLabel lab_10 = new JLabel("十进制:");
static JLabel lab_8 = new JLabel("八进制:");
static JLabel lab_16 = new JLabel("十六进制:");
static JLabel lab_32 = new JLabel("三十二进制:");

static JLabel lab_url = new JLabel("URL编码");
static JLabel lab_utf = new JLabel("UTF-8编码");
static JLabel lab_ascii = new JLabel("ASCII编码");
static JLabel lab_base64 = new JLabel("Base64编码");
static JLabel lab_unicode = new JLabel("Unicode编码");

static JTextField jtf_2 = new JTextField(50);
static JTextField jtf_4 = new JTextField(50);
static JTextField jtf_8 = new JTextField(50);
static JTextField jtf_10 = new JTextField(50);
static JTextField jtf_16 = new JTextField(50);
static JTextField jtf_32 = new JTextField(50);

static JButton jb_2 = new JButton("二进制转!");
static JButton jb_4 = new JButton("四进制转!");
static JButton jb_8 = new JButton("八进制转!");
static JButton jb_10 = new JButton("十进制转!");
static JButton jb_16 = new JButton("十六进制转!");
static JButton jb_32 = new JButton("三十二进制转!");

static JButton jb_decode = new JButton("进行解码");
static JButton jb_encode = new JButton("进行编码");
static JTextField jTextField_src = new JTextField("在此输入想要编码或者解码的内容");
static JTextField jTextField_unicode = new JTextField();
static JTextField jTextField_url = new JTextField();
static JTextField jTextField_utf = new JTextField();
static JTextField jTextField_ascii = new JTextField();
static JTextField jTextField_base64 = new JTextField();



static JTabbedPane tp = new JTabbedPane();
static JPanel jpa_system = new JPanel();
static JPanel jpa_trans = new JPanel();

static double toDouble(char x) {
if(x >= '0' && x<='9')
return x - '0';
else if(x >= 'a' && x <= 'z')
return x - 'a' + 10 ;
else
return x -'A' + 10;
}

static char toChar(int x) {
char x1;
if(x >= 0 && x <= 9) {
x1 = (char)(x + 48);
}
else {
int z = x - 10;
x1 = (char)(z + 65);
}
return x1;
}

static String transRadix(String str, int srcNum, int desNum) {
char[] chs = new char[str.length()];
chs = str.toCharArray();
double product = 1.0; // 进制的幂
double deca = 0; // 存为十进制中间转换
for(int i = chs.length - 1; i >= 0; i--) {
char ch = chs[i];
deca += toDouble(ch) * product;
product *= srcNum;
}
char[] relusts = new char[100];
String relStr = "";
int num = 0;
do {
double reout = deca % desNum;
relusts[num++] = toChar((int)reout);
deca = deca / desNum;
}while(deca >= 1); // 当十进制中间数大于1时继续计算
relStr = relStr.valueOf(relusts,0,num);
relStr.trim();
StringBuffer strBuf = new StringBuffer(relStr);
relStr = strBuf.reverse().toString();
return relStr;
}

static class myLisenter implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
String str = actionEvent.getActionCommand();
String str2;
double num;
if (str.equals(jb_2.getText())) {
str2 = jtf_2.getText();
jtf_4.setText(transRadix(str2,2,4));
jtf_8.setText(transRadix(str2,2,8));
jtf_10.setText(transRadix(str2,2,10));
jtf_16.setText(transRadix(str2,2,16));
jtf_32.setText(transRadix(str2,2,32));
}
else if(str.equals(jb_4.getText())) {
str2 = jtf_4.getText();
jtf_2.setText(transRadix(str2,4,2));
jtf_8.setText(transRadix(str2,4,8));
jtf_10.setText(transRadix(str2,4,10));
jtf_16.setText(transRadix(str2,4,16));
jtf_32.setText(transRadix(str2,4,32));
}
else if(str.equals(jb_8.getText())) {
str2 = jtf_8.getText();
jtf_2.setText(transRadix(str2,8,2));
jtf_4.setText(transRadix(str2,8,4));
jtf_10.setText(transRadix(str2,8,10));
jtf_16.setText(transRadix(str2,8,16));
jtf_32.setText(transRadix(str2,8,32));
}
else if(str.equals(jb_10.getText())) {
str2 = jtf_10.getText();
jtf_2.setText(transRadix(str2,10,2));
jtf_4.setText(transRadix(str2,10,4));
jtf_8.setText(transRadix(str2,10,8));
jtf_16.setText(transRadix(str2,10,16));
jtf_32.setText(transRadix(str2,10,32));
}
else if(str.equals(jb_16.getText())) {
str2 = jtf_16.getText();
jtf_2.setText(transRadix(str2,16,2));
jtf_4.setText(transRadix(str2,16,4));
jtf_8.setText(transRadix(str2,16,8));
jtf_10.setText(transRadix(str2,16,10));
jtf_32.setText(transRadix(str2,16,32));
}
else if(str.equals(jb_32.getText())) {
str2 = jtf_32.getText();
jtf_2.setText(transRadix(str2,32,2));
jtf_4.setText(transRadix(str2,32,4));
jtf_8.setText(transRadix(str2,32,8));
jtf_10.setText(transRadix(str2,32,10));
jtf_16.setText(transRadix(str2,32,16));
}
else if(str.equals(jb_encode.getText())) {
str2 = jTextField_src.getText();
jTextField_ascii.setText(asciiEncode(str2));
jTextField_base64.setText(baseEncode(str2));
jTextField_unicode.setText(unicodeEncode(str2));
jTextField_url.setText(urlEncode(str2));
jTextField_utf.setText(utfEncode(str2));
}
else if(str.equals(jb_decode.getText())) {
str2 = jTextField_src.getText();
try {
jTextField_ascii.setText(asciiDecode(str2));
}catch (Exception e) {
jTextField_ascii.setText("ASCII码转码失败");
System.out.println(e);
}
try {
jTextField_base64.setText(baseDecode(str2));
}catch (Exception e) {
System.out.println(e);
}
try {
jTextField_unicode.setText(unicodeDecode(str2));
}catch (Exception e) {
jTextField_unicode.setText("Unicode转码失败");
System.out.println(e);
}
try {
jTextField_url.setText(urlDecode(str2));
}catch (Exception e) {
jTextField_url.setText("URL转码失败");
System.out.println(e);
}
try {
jTextField_utf.setText(utfDecode(str2));
}catch (Exception e) {
jTextField_utf.setText("UTF-8转码失败");
System.out.println(e);
}
}
}
}

static String unicodeEncode(String strSrc) {
StringBuffer output = new StringBuffer();
for (int i = 0; i < strSrc.length(); i++)
{
output.append("\\u" +Integer.toString(strSrc.charAt(i), 16));
}
return output.toString();
}
static String unicodeDecode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
}
else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}

static String utfEncode(String strSrc) {
StringBuffer output = new StringBuffer();
for (int i = 0; i < strSrc.length(); i++)
{
output.append("&#x" + Integer.toString(strSrc.charAt(i), 16) + ";");
}
return output.toString();
}
static String utfDecode(String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("&#x", start + 3);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 3, dataStr.length());
charStr = charStr.replaceAll(";", "");
}
else {
charStr = dataStr.substring(start + 3, end);
charStr = charStr.replaceAll(";", "");
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}

static String asciiEncode(String strSrc) {
StringBuilder sb = new StringBuilder();
char[] ch = strSrc.toCharArray();
for (int i = 0; i < ch.length; i++) {
sb.append(Integer.valueOf(ch[i]).intValue()).append(" ");
}
String[] chars = sb.toString().split(" ");
StringBuffer strOut = new StringBuffer("\\u");
for (int i = 0; i < chars.length; i++) {
strOut.append(chars[i]);
if(i == chars.length - 1)
break;
strOut.append("\\u");
}
return strOut.toString();
}

static String asciiDecode(String strDes) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = strDes.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = strDes.substring(start + 2, strDes.length());
}
else {
charStr = strDes.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 10); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
public static String urlEncode(String str) {
String result = "";
if (null == str) {
return "";
}
try {
result = java.net.URLEncoder.encode(str, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static String urlDecode(String str) {
String result = "";
if (null == str) {
return "";
}
try {
result = java.net.URLDecoder.decode(str, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

static String baseEncode(String strSrc) {
Base64.Encoder encoder = Base64.getEncoder();
String encodedText = "Base64加密失败";
try{
byte[] textByte = strSrc.getBytes("UTF-8");
encodedText = encoder.encodeToString(textByte);
}catch (Exception e) {
System.out.println(e);
}
return encodedText;
}

static String baseDecode(String strSrc) {
Base64.Decoder decoder = Base64.getDecoder();
String decodedText = "Base64解密失败";
try{
decodedText = (new String(decoder.decode(strSrc), "UTF-8"));
}catch (Exception e){
System.out.println(e);
}
return decodedText;
}


public static void main(String[] args) {
jfrm.setSize(515,440);
jfrm.setIconImage(im);
ct.setLayout(null);
jpa_system.setLayout(null);
jpa_trans.setLayout(null);

lab_2.setBounds(20,10,80,20);
lab_4.setBounds(20,60,80,20);
lab_8.setBounds(20,110,80,20);
lab_10.setBounds(20,160,80,20);
lab_16.setBounds(20,210,80,20);
lab_32.setBounds(20,260,80,20);

jtf_2.setBounds(100,10,250,20);
jtf_4.setBounds(100,60,250,20);
jtf_8.setBounds(100,110,250,20);
jtf_10.setBounds(100,160,250,20);
jtf_16.setBounds(100,210,250,20);
jtf_32.setBounds(100,260,250,20);

jb_2.setBounds(360,10,110,20);
jb_4.setBounds(360,60,110,20);
jb_8.setBounds(360,110,110,20);
jb_10.setBounds(360,160,110,20);
jb_16.setBounds(360,210,110,20);
jb_32.setBounds(360,260,110,20);

jpa_system.add(lab_2);
jpa_system.add(lab_4);
jpa_system.add(lab_8);
jpa_system.add(lab_10);
jpa_system.add(lab_16);
jpa_system.add(lab_32);

jpa_system.add(jtf_2);
jpa_system.add(jtf_4);
jpa_system.add(jtf_8);
jpa_system.add(jtf_10);
jpa_system.add(jtf_16);
jpa_system.add(jtf_32);

jpa_system.add(jb_2);
jpa_system.add(jb_4);
jpa_system.add(jb_8);
jpa_system.add(jb_10);
jpa_system.add(jb_16);
jpa_system.add(jb_32);

jb_2.addActionListener(new myLisenter());
jb_4.addActionListener(new myLisenter());
jb_8.addActionListener(new myLisenter());
jb_10.addActionListener(new myLisenter());
jb_16.addActionListener(new myLisenter());
jb_32.addActionListener(new myLisenter());



jb_encode.setBounds(10,15,90,20);
jb_decode.setBounds(10,40,90,20);
jTextField_src.setBounds(110,10,350,50);
jTextField_base64.setBounds(110,60,350,50);
jTextField_unicode.setBounds(110,110,350,50);
jTextField_url.setBounds(110,160,350,50);
jTextField_utf.setBounds(110,210,350,50);
jTextField_ascii.setBounds(110,260,350,50);

lab_base64.setBounds(20,70,80,40);
lab_unicode.setBounds(20,120,80,40);
lab_url.setBounds(20,170,80,40);
lab_utf.setBounds(20,220,80,40);
lab_ascii.setBounds(20,270,80,40);

jpa_trans.add(jTextField_ascii);
jpa_trans.add(jTextField_base64);
jpa_trans.add(jTextField_src);
jpa_trans.add(jTextField_unicode);
jpa_trans.add(jTextField_url);
jpa_trans.add(jTextField_utf);

jpa_trans.add(lab_ascii);
jpa_trans.add(lab_base64);
jpa_trans.add(lab_unicode);
jpa_trans.add(lab_url);
jpa_trans.add(lab_utf);

jpa_trans.add(jb_decode);
jpa_trans.add(jb_encode);

writer1.setBounds(350,310,250,40);
email1.setBounds(350,330,250,40);

jpa_trans.add(writer1);
jpa_trans.add(email1);

writer2.setBounds(350,310,250,40);
email2.setBounds(350,330,250,40);
jpa_system.add(writer2);
jpa_system.add(email2);


jb_decode.addActionListener(new myLisenter());
jb_encode.addActionListener(new myLisenter());

tp.setBounds(0,0,500,400);
tp.add("进制转换",jpa_system);
tp.add("编码转换",jpa_trans);
ct.add(tp);

jfrm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jfrm.setVisible(true);


}
}
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • © 2018-2020 Li XiangLan
  • PV: UV:

请我喝杯咖啡吧~