DEV Community

Mansour Moufid
Mansour Moufid

Posted on

Encode and decode FourCC codes, in Python

Convert the string "H264" to an integer code:

sum([ord(c) << (i * 8) for i, c in enumerate("H264")])
Enter fullscreen mode Exit fullscreen mode

Convert the integer code (0x34363248) back to a string:

''.join([chr((0x34363248 >> (i * 8)) & 0xff) for i in range(4)])
Enter fullscreen mode Exit fullscreen mode

See this gist: https://gist.github.com/eliteraspberries/b017f998c97c395881f0d2f05a08ec60

Top comments (0)