Convert
python
October 8, 20221 min read

Convert SHA-256 string to integer bits in python

As we've seen in earlier posts, you can create an SHA256 hash of a string and generate a hash in string type. But, what if you had to create an integer out of it? Practical real-world use-cases can be - if you have a unique integer ID that you have in your database which you can map.

import hashlib
import uuid

hash = hashlib.sha256('some string'.encode('utf-8'))
a = uuid.UUID(hash.hexdigest()[::2])
print(a)

In the above code, you can see that we are passing the hash string to the uuid.UUID, which returns the UUID value of the SHA-256 hash.

The output of the above code will be a UUID-

6d3430dd-c092-741d-0452-f815d9b1433f

I'm glad that you found this article to create UUID from a SHA-256 hash string useful. Happy Coding.

Share this blog
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
September 14, 20224 min read
Next Article