Skip to content

Instantly share code, notes, and snippets.

@iacchus
Last active December 10, 2017 18:52
Show Gist options
  • Save iacchus/a9f95d486c38d35e5de415e685189002 to your computer and use it in GitHub Desktop.
Save iacchus/a9f95d486c38d35e5de415e685189002 to your computer and use it in GitHub Desktop.
Python 3 ASCII to Full Widh chars

Snippet to convert ascii chars to full-width characters

First one:

>>> def widen_str(string):
...     return(''.join([chr(0xFEE0 + ord(char)) for char in string]))
...
>>> widen_str('Hello, World!')
'Hello,\uff00World!'
>>> # kinda works
...
>>>

Another One

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

WIDE_MAP = dict((i, i + 0xFEE0) for i in range(0x21, 0x7F))
WIDE_MAP[0x20] = 0x3000

def widen(s):
    """
    Convert all ASCII characters to the full-width counterpart.

    >>> print widen('test, Foo!')
    test, Foo!
    >>>
    """
    return s.translate(WIDE_MAP)

#while True:
#    line = sys.stdin.readline()
#    if not line: break
#    sys.stdout.write(widen(line.decode('utf-8')).encode('utf-8'))

print(widen('the wizards club'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment