Created
October 14, 2021 23:23
-
-
Save barryjburns/3f87432cd3a2e233e4fe269759053692 to your computer and use it in GitHub Desktop.
String to hex using map, reduce, and operator.{mod,concat} in 1 very Pythonic, functional, iterative line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
A compact and, if I may say so, exceptionally "Pythonic" function/script | |
to convert strings to the hex representation of their UTF-8 encoding. | |
Were one so inclined, one could use it to assign, oh, site local | |
IPv6 addresses, for one... 😉 | |
-- luv, pluggo 💙 | |
''' | |
import functools, operator, itertools, sys | |
str2hex = lambda _: functools.reduce(operator.concat, map(operator.mod, itertools.repeat('%2.2x'), _.encode('utf-8'))) | |
if __name__ == '__main__': print(*map(str2hex, sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment