Method 1:
/**
* Special String Conversion
* @ Class Name FullCharConverter
* @ Author v-jiangwei
* @ Create In 2012-8-24
*/
Public class FullCharConverter {
/**
* Conversion functions from full-width to half-width
* @ Methods Name full2HalfChange
* @ Create In 2012-8-24 By v-jiangwei
* @ Param QJstr
* @ Return String
*/
Public static final String full2HalfChange (String QJstr)
{
StringBuffer outStrBuf = new StringBuffer ("");
String Tstr = "";
Byte [] B = null;
For (int I = 0; I <QJstr. length (); I ++ ){
Tstr = QJstr. substring (I, I + 1 );
// Convert fullwidth spaces to halfwidth Spaces
If (Tstr. equals ("")){
OutStrBuf. append ("");
Continue;
}
Try {
B = Tstr. getBytes ("unicode ");
// Obtain unicode bytes
If (B [2] =-1 ){
// Indicates the fullwidth
B [3] = (byte) (B [3] + 32 );
B [2] = 0;
OutStrBuf. append (new String (B, "unicode "));
} Else {
OutStrBuf. append (Tstr );
}
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
} // End.
Return outStrBuf. toString ();
}
/**
* Halfwidth to fullwidth
* @ Methods Name half2Fullchange
* @ Create In 2012-8-24 By v-jiangwei
* @ Param QJstr
* @ Return String
*/
Public static final String half2Fullchange (String QJstr)
{
StringBuffer outStrBuf = new StringBuffer ("");
String Tstr = "";
Byte [] B = null;
For (int I = 0; I <QJstr. length (); I ++ ){
Tstr = QJstr. substring (I, I + 1 );
If (Tstr. equals ("")){
// Halfwidth Space
OutStrBuf. append (Tstr );
Continue;
}
Try {
B = Tstr. getBytes ("unicode ");
If (B [2] = 0 ){
// Halfwidth
B [3] = (byte) (B [3]-32 );
B [2] =-1;
OutStrBuf. append (new String (B, "unicode "));
} Else {
OutStrBuf. append (Tstr );
}
Return outStrBuf. toString ();
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Return outStrBuf. toString ();
}
/**
* @ Methods Name main
* @ Create In 2012-8-24 By v-jiangwei
* @ Param args
* Void
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
String QJstr = "814 ";
String result = FullCharConverter. full2HalfChange (QJstr );
System. out. println (QJstr );
System. out. println (result );
System. out. println ("------------------------------------");
// Halfwidth to fullwidth
String str = "G45 High Speed ";
System. out. println (str );
System. out. println (FullCharConverter. half2Fullchange (str ));
}
}
Method 2:
/**
* Halfwidth to fullwidth
* @ Param input String.
* @ Return full-width string.
*/
Public static String ToSBC (String input ){
Char c [] = input. toCharArray ();
For (int I = 0; I <c. length; I ++ ){
If (c [I] = ''){
C [I] = '\ u3000 ';
} Else if (c [I] <'\ 177 '){
C [I] = (char) (c [I] + 65248 );
}
}
Return new String (c );
}
/**
* Full-width to half-width
* @ Param input String.
* @ Return half-width string
*/
Public static String ToDBC (String input ){
Char c [] = input. toCharArray ();
For (int I = 0; I <c. length; I ++ ){
If (c [I] = '\ u3000 '){
C [I] = ''; www.2cto.com
} Else if (c [I]> '\ uff00' & c [I] <' \ uff5f '){
C [I] = (char) (c [I]-65248 );
}
}
String returnString = new String (c );
Return returnString;
}
Public static void main (String [] args ){
String QJstr = "G45 High Speed ";
String result = StringUtil. ToDBC (QJstr );
System. out. println (result );
}