1
package com.breeze.util;
2
3
public class ChangeCode
{
4
5
public static void main(String[] args)
{
6
7
String QJstr = "abcdefghijklmnopqrstuvwxyz,.'?";
8
String QJstr2="abcdefghijklmnopqrstuvwxyz";
9
10
String result1 = Q2B(QJstr2);
11
String result2 = B2Q(QJstr);
12
13
System.out.println(result1);
14
System.out.println("\n"+result2);
15
}
16
17
private static String Q2B(String QJstr)
{// 全角-->半角
18
String outStr = "";
19
String Tstr = "";
20
byte[] b = null;
21
22
for (int i = 0; i < QJstr.length(); i++)
{
23
try
{
24
Tstr = QJstr.substring(i, i + 1);
25
b = Tstr.getBytes("unicode");
26
} catch (java.io.UnsupportedEncodingException e)
{
27
e.printStackTrace();
28
}
29
30
if (b[3] == -1)
{
31
b[2] = (byte) (b[2] + 32);
32
b[3] = 0;
33
34
try
{
35
outStr = outStr + new String(b, "unicode");
36
} catch (java.io.UnsupportedEncodingException e)
{
37
e.printStackTrace();
38
}
39
} else
40
outStr = outStr + Tstr;
41
}
42
43
return outStr;
44
}
45
46
private static String B2Q(String QJstr)
{// 半角-->全角
47
String outStr = "";
48
String Tstr = "";
49
byte[] b = null;
50
51
for (int i = 0; i < QJstr.length(); i++)
{
52
try
{
53
Tstr = QJstr.substring(i, i + 1);
54
b = Tstr.getBytes("unicode");
55
} catch (java.io.UnsupportedEncodingException e)
{
56
e.printStackTrace();
57
}
58
59
System.out.println(Tstr);
60
System.out.println("b[0]="+b[0]);
61
System.out.println("b[1]="+b[1]);
62
System.out.println("b[2]="+b[2]);
63
System.out.println("b[3]="+b[3]);
64
if (b[3] == 0)
{
65
b[2] = (byte) (b[2] - 32);
66
b[3] = -1;
67
68
try
{
69
outStr = outStr + new String(b, "unicode");
70
} catch (java.io.UnsupportedEncodingException e)
{
71
e.printStackTrace();
72
}
73
} else
74
outStr = outStr + Tstr;
75
}
76
77
return outStr;
78
}
79
}
80