Skip to content

Instantly share code, notes, and snippets.

@mansourmoufid
Last active March 31, 2022 22:51
Show Gist options
  • Save mansourmoufid/b017f998c97c395881f0d2f05a08ec60 to your computer and use it in GitHub Desktop.
Save mansourmoufid/b017f998c97c395881f0d2f05a08ec60 to your computer and use it in GitHub Desktop.
class FourCC:
def __init__(self, code):
if isinstance(code, str):
code = sum([ord(c) << (i * 8) for i, c in enumerate(code)])
self.code = int(code)
def __int__(self):
return self.code
def __str__(self):
return ''.join([chr((self.code >> (i * 8)) & 0xff) for i in range(4)])
def __repr__(self):
return '<FourCC int={} hex={} str={}>'.format(
self.code,
hex(self.code),
str(self),
)
def __eq__(self, other):
if isinstance(other, FourCC):
return self.code == other.code
elif isinstance(other, str):
return str(self) == other
else:
return self.code == other
if __name__ == '__main__':
import sys
for x in sys.argv[1:]:
try:
code = int(x)
except ValueError:
try:
code = int(x, 16)
except ValueError:
code = x
print(repr(FourCC(code)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment