Skip to content

Instantly share code, notes, and snippets.

@cas--
Created June 13, 2023 11:48
Show Gist options
  • Save cas--/8e3083291cbef3089a673fd849ac39fc to your computer and use it in GitHub Desktop.
Save cas--/8e3083291cbef3089a673fd849ac39fc to your computer and use it in GitHub Desktop.
OFFSET = 127462 - ord('A')
def unicode_flag_to_country_code(code_points: list) -> str:
"""Converts unicode emoji flag to country code string
Args:
code_points: A list of unicode code points representing a
country flag
Returns:
The two-letter country code
"""
return ''.join(chr(int(char, 16) - OFFSET) for char in (code_points))
def country_code_to_unicode_flag(country_code: str) -> list:
"""Converts a country code string to unicode code points
Args:
country_code: A two letter IANA BCP 47 region code
Returns:
A list of unicode code points for the country flag
"""
return [hex(ord(char) + OFFSET) for char in (country_code)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment