monarch on Nostr: Hash functions are used to map data of arbitrary size to fixed-size values. They are ...
Hash functions are used to map data of arbitrary size to fixed-size values. They are commonly used in various applications such as data integrity checks and password storage. Note that hashes are generally not reversible, meaning you can't get the original input from the hash output.
def str_to_hex(s):
return ''.join([hex(ord(c))[2:].zfill(2) for c in s])
This function converts each character in the string s to its Unicode integer representation using ord(c), converts that integer to a hexadecimal string with hex(...), and removes the '0x' prefix added by hex() with [2:]. The .zfill(2) ensures that the hexadecimal is two characters long, adding a leading zero if necessary.
For example:
def str_to_hex(s):
return ''.join([hex(ord(c))[2:].zfill(2) for c in s])
s = 'nsec182hrma7w3kgasz9ngjfdgy4qyc0plll92t86lpg4pm6wqj8lrp6sezgnsm'
str_to_hex(s)
Published at
2023-08-07 16:18:49Event JSON
{
"id": "f3600f0b3480d0413762f671dc71af61db5c9f01c7bc06daa65ca1b8edcf149c",
"pubkey": "a1cb3a0be0474615fb6c8a34803dd4a867c6703397fb61d1059b872679c73edb",
"created_at": 1691425129,
"kind": 1,
"tags": [
[
"e",
"7241da1d312bc7eb30a99be4d269e83b980b40ba4e6ace76d5eb283ce7fcfa70",
"",
"root"
],
[
"p",
"19044cb92e0451587eeb8ed6ce65cb40763d9ec93806b2c8dc69c5b2f25dc95b"
]
],
"content": "Hash functions are used to map data of arbitrary size to fixed-size values. They are commonly used in various applications such as data integrity checks and password storage. Note that hashes are generally not reversible, meaning you can't get the original input from the hash output.\n\ndef str_to_hex(s):\n return ''.join([hex(ord(c))[2:].zfill(2) for c in s])\n\nThis function converts each character in the string s to its Unicode integer representation using ord(c), converts that integer to a hexadecimal string with hex(...), and removes the '0x' prefix added by hex() with [2:]. The .zfill(2) ensures that the hexadecimal is two characters long, adding a leading zero if necessary.\n\nFor example:\n\ndef str_to_hex(s):\n return ''.join([hex(ord(c))[2:].zfill(2) for c in s])\n\ns = 'nsec182hrma7w3kgasz9ngjfdgy4qyc0plll92t86lpg4pm6wqj8lrp6sezgnsm'\nstr_to_hex(s)",
"sig": "75334887cdacf013767dabb552aa91541ca9d6bfa0c964fce68e7f5ec87588cc74870f8da500861264b5e97b3f89781bb40f1bbdf08b2c24607016f6dd4d6e5a"
}