Python implements the full Angle half Angle conversion method

  • 2020-04-02 13:54:17
  • OfStack

In this paper, an example is given to illustrate the method of full Angle and half Angle conversion in Python. As follows:

I. overview of full-angle half-angle conversion:

Full-angle characters unicode encoding from 65281 to 65374 (hex 0xFF01 to 0xFF5E)
Half corner character unicode encoding from 33~126 (hex 0x21~ 0x7E)
The space is special, the full Angle is 12288 (0x3000), and the half Angle is 32 (0x20).
Besides, the full/half Angle sort by unicode code is corresponding in order except for the space
So you can directly use the +- method to deal with non-space data, the space alone

Ii. Full Angle half Angle:

The implementation code is as follows:


def strQ2B(ustring):
  """ Turn the full Angle of the string half way """
  rstring = ""
  for uchar in ustring:
    inside_code=ord(uchar)
    if inside_code==0x3000:
      inside_code=0x0020
    else:
      inside_code-=0xfee0
    if inside_code<0x0020 or inside_code>0x7e:   # Not half - corner characters return the original character after the turn 
      rstring += uchar
    rstring += unichr(inside_code)
  return rstring

Three, half Angle to full Angle:

The implementation code is as follows:


def strB2Q(ustring):
  """ Turn half of a string into full Angle """
  rstring = ""
  for uchar in ustring:
    inside_code=ord(uchar)
    if inside_code<0x0020 or inside_code>0x7e:   # Returns the original character if it is not a half - corner character 
      rstring += uchar
    if inside_code==0x0020: # The formula for all angles and half angles except for Spaces is : Half Angle = The Angle of -0xfee0
      inside_code=0x3000
    else:
      inside_code+=0xfee0
    rstring += unichr(inside_code)
  return rstring

Iv. Test code


a = strB2Q("abc12345")
print a
b = strQ2B(a)
print b

Output:


 abc12345 
abc12345

Interested friends can debug the run, I believe there will be a certain harvest.


Related articles: