Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to hex-string with specific format

I am new to python and have following problem: I need to convert an integer to a hex string with 6 bytes.

e.g. 281473900746245 --> "\xFF\xFF\xBF\xDE\x16\x05"

The format of the hex-string is important. The length of the int value is variable.

The format '0xffffbf949309L' don't work for me. (I get this with hex(int-value))


My final solution (after some "playing") is:

def _tohex(self, int_value):
    data_ = format(int_value, 'x')

    result = data_.rjust(12, '0')
    hexed = unhexlify(result)

    return hexed

Thank you for all the help!

like image 724
Oxymoron Avatar asked Jul 29 '11 14:07

Oxymoron


People also ask

How to convert integer to Hex string in Java?

The Integer.toHexString () method in Java converts Integer to hex string. Let’s say the following are our integer values. int val1 = 5; int val2 = 7; int val3 = 13; Convert the above int values to hex string.

How to convert integer to hexadecimal in Python?

Another option is to use the built-in function format (), which can convert an integer to a hexadecimal string using the x format specification. If you use #x, the hexadecimal string is prefixed by 0x. If you use X or #X, you can get an uppercase hexadecimal string. 3. Using f-strings Starting with Python 3.6, you can use f-strings.

How do I use the hexadecimal output from format-Hex?

The hexadecimal output from Format-Hex shows the values of each character in the string. Example 2: Find a file type from hexadecimal output. This example uses the hexadecimal output to determine the file type. The cmdlet displays the file's full path and the hexadecimal values.

How to convert number to string using %X format specifier in C?

In C / C++ there is a format specifier %X. It prints the value of some variable into hexadecimal form. We have used this format specifier to convert number into a string by using sprintf () function. Step 1:Take a number from the user Step 2: Make a string after converting number using %X format specifier Step 3: Print the result. Step 4: End


3 Answers

There might be a better solution, but you can do this:

x = 281473900746245
decoded_x = hex(x)[2:].decode('hex') # value: '\xff\xff\xbf\xde\x16\x05'

Breakdown:

hex(x)                     # value: '0xffffbfde1605'
hex(x)[2:]                 # value: 'ffffbfde1605'
hex(x)[2:].decode('hex')   # value: '\xff\xff\xbf\xde\x16\x05'

Update:

Per @multipleinstances and @Sven's comments, since you might be dealing with long values, you might have to tweak the output of hex a little bit:

format(x, 'x')     # value: 'ffffbfde1605'

Sometimes, however, the output of hex might be an odd-length, which would break decode, so it'd probably be better to create a function to do this:

def convert(int_value):
   encoded = format(int_value, 'x')

   length = len(encoded)
   encoded = encoded.zfill(length+length%2)

   return encoded.decode('hex')
like image 66
Manny D Avatar answered Oct 21 '22 02:10

Manny D


In Python 3.2 or above, you can use the to_bytes() method of the interger.

>>> i = 281473900746245       
>>> i.to_bytes((i.bit_length() + 7) // 8, "big")
b'\xff\xff\xbf\xde\x16\x05'
like image 31
Sven Marnach Avatar answered Oct 21 '22 01:10

Sven Marnach


If you don't use Python 3.2 (I'm pretty sure you don't), consider the next approach:

>>> i = 281473900746245
>>> hex_repr = []
>>> while i:
...     hex_repr.append(struct.pack('B', i & 255))
...     i >>= 8
...
>>> ''.join(reversed(hex_repr))
'\xff\xff\xbf\xde\x16\x05'
like image 36
Roman Bodnarchuk Avatar answered Oct 21 '22 00:10

Roman Bodnarchuk