Java full Angle half Angle character relationships and conversion details

  • 2020-04-01 02:32:21
  • OfStack

If you understand the relationship between full-angle and half-angle characters in Java

Then the transition between them is not a matter at all.

The relationship between full and half - corner characters

You can see all the characters in Java and the corresponding encoded values in the following program


    public static void main(String[] args) {
        for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; ++i) {
            System.out.println(i + "    " + (char)i);
        }
    }

 

You can see that from the output

1. Half - corner characters start at 33 and end at 126
2. The full Angle character corresponding to the half Angle character starts from 65281 to 65374
3. The space in the half corner is 32. The corresponding space in the full corner is 12288
The relationship between half Angle and full Angle is obvious. The character offset except the space is 65248(65281-33 = 65248).

The Java language implements the conversion between full and half corners

Now that you understand the relationships between full - horn characters, let's look at the Java implementation



    private static String fullWidth2halfWidth(String fullWidthStr) {
        if (null == fullWidthStr || fullWidthStr.length() <= 0) {
            return "";
        }
        char[] charArray = fullWidthStr.toCharArray();
        //Char array traversal for full - Angle conversion
        for (int i = 0; i < charArray.length; ++i) {
            int charIntValue = (int) charArray[i];
            //If the conversion relation is met, the offset is subtracted from the corresponding subscripts. If it is a space, do the conversion directly
            if (charIntValue >= 65281 && charIntValue <= 65374) {
                charArray[i] = (char) (charIntValue - 65248);
            } else if (charIntValue == 12288) {
                charArray[i] = (char) 32;
            }
        }
        return new String(charArray);
    }


Related articles: