How to achieve Java full Angle half Angle character conversion

  • 2020-04-01 01:26:01
  • OfStack

 
package com.whatycms.common.util; 
import org.apache.commons.lang.StringUtils; 
/** 
* <PRE> 
*  Provides full Angle to the string -> Half an Angle, half an Angle -> The Angle of transformation  
* </PRE> 
*/ 
public class BCConvert { 
 
static final char DBC_CHAR_START = 33; //Half Angle!
 
static final char DBC_CHAR_END = 126; //Half Angle ~
 
static final char SBC_CHAR_START = 65281; //The Angle!
 
static final char SBC_CHAR_END = 65374; //The Angle of ~
 
static final int CONVERT_STEP = 65248; //Full Angle half Angle conversion interval
 
static final char SBC_SPACE = 12288; //Full corner space 12288
 
static final char DBC_SPACE = ' '; //Half Angle space
/** 
* <PRE> 
*  Half Angle character -> Full Angle character conversion  
*  I'm just dealing with Spaces, ! to ˜ Between the characters, ignore the others  
* </PRE> 
*/ 
private static String bj2qj(String src) { 
if (src == null) { 
return src; 
} 
StringBuilder buf = new StringBuilder(src.length()); 
char[] ca = src.toCharArray(); 
for (int i = 0; i < ca.length; i++) { 
if (ca[i] == DBC_SPACE) { //If it is a half space, use the full space instead
buf.append(SBC_SPACE); 
} else if ((ca[i] >= DBC_CHAR_START) && (ca[i] <= DBC_CHAR_END)) { //Character is! The visible character between ~ and
buf.append((char) (ca[i] + CONVERT_STEP)); 
} else { //Do not do anything with characters other than Spaces and other visible characters in the ASCII table
buf.append(ca[i]); 
} 
} 
return buf.toString(); 
} 
/** 
* <PRE> 
*  The Angle of character -> Half corner character conversion  
*  Only full Angle Spaces, full Angle! To the full Angle ~ between the characters, ignore the others  
* </PRE> 
*/ 
public static String qj2bj(String src) { 
if (src == null) { 
return src; 
} 
StringBuilder buf = new StringBuilder(src.length()); 
char[] ca = src.toCharArray(); 
for (int i = 0; i < src.length(); i++) { 
if (ca[i] >= SBC_CHAR_START && ca[i] <= SBC_CHAR_END) { //If at full Angle! To the whole Angle to the interval
buf.append((char) (ca[i] - CONVERT_STEP)); 
} else if (ca[i] == SBC_SPACE) { //If it's a full Angle space
buf.append(DBC_SPACE); 
} else { //Do not handle full corner space, full corner! To a character outside the full Angle ~ interval
buf.append(ca[i]); 
} 
} 
return buf.toString(); 
} 
public static void main(String[] args) { 
System.out.println(StringUtils.trimToEmpty(" a,b ,c ")); 
String s = "nihao Hk | nihehe, 787 "; 
s=BCConvert.qj2bj(s); 
System.out.println(s); 
System.out.println(BCConvert.bj2qj(s)); 
} 
} 

Console output is as follows:
 
a,b ,c 
nihaohk | nihehe , .  78 7 
 Nihaohk | nihehe. 787  

Related articles: