Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert File to HEX String Python

How would I convert a file to a HEX string using Python? I have searched all over Google for this, but can't seem to find anything useful.

like image 425
Zac Brown Avatar asked Oct 18 '10 23:10

Zac Brown


People also ask

How do you hex a string in Python?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.

How do I read a binary file as hex in python?

Use the binascii Module to Convert Binary to Hex in Python The binascii module needs to be manually imported to the Python code in order for this method to work. This method opens a text file, takes in the content of the file, and can return the hex value of the given data in the file using the hexlify() function.

How do you convert int to hex in python?

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.


1 Answers

import binascii filename = 'test.dat' with open(filename, 'rb') as f:     content = f.read() print(binascii.hexlify(content)) 
like image 107
unutbu Avatar answered Sep 23 '22 03:09

unutbu