How to join hex values - python

I am reading four bytes from file
I would like to join them
g = f.read(60)
f.seek (60)
k60 =f.read(1)
print('byte60',k60)
k61 =f.read(1)
print('byte61',k61)
k62 =f.read(1)
print('byte62',k62)
k63 =f.read(1)
print('byte63',k63)
print(k63,k62,k61,k60)
print (b''.join([k63,k62,k61,k60]))
Result is:
b'\x00\x00\x00\x80'
I would like to receive:
00000080

You to convert a byte string to its hex representation, you can use the hexlify() method from the binascii module:
>>> from binascii import hexlify
>>> ...
>>> raw = b''.join([k63,k62,k61,k60])
>>> print(hexlify(raw))
b'00000080'
>>> print(hexlify(raw).decode('ascii') # if you want to convert it to a string
00000080
The same could be accomplished by using codecs.encode(raw, 'hex').

Related

how to convert string of bytes string into string using python

I got a string of bytes string like below:
string1 = "b'\xe6\x88\x91\xe4\xbb\xac \xe7\xb4\xa2\xe8\xa6\x81 \xe6\x8e\xa8\xe5\xb9\xbf \xe7\x9a\x84 \xe6\x98\xaf\xe4\xb8\x80 \xe5\xbe\x97 \xe6\x96\xb9\xe6\x96\xb9 \xe6\x96\xb9\xe8\xa8\x80 \xe4\xb8\xba\xe5\x9f\xba \xe7\xa1\x80 \xe6\x96\xb9\xe8\xa8\x80 \xe4\xb8\x80 \xe5\x8c\x97\xe4\xba\xac \xe5\xb7\xb2 \xe5\x9b\xa0 \xe4\xb8\xba \xe6\xa0\x87\xe5\x87\x86 \xe7\x9a\x84 \xe6\x99\xae\xe9\x80\x9a \xe8\xaf\x9d \xe4\xbb\x96 \xe4\xbb\x8e \xe5\x84\xbf\xe7\xab\xa5 \xe6\x97\xb6\xe4\xbb\xa3 \xe8\xb5\xb7 \xe5\xb0\xb1 \xe5\x96\x9c\xe6\xac\xa2 \xe4\xb8\x8b \xe5\x9b\xb4\xe6\xa3\x8b \xe5\x9c\xa8 \xe5\x8d\x81\xe4\xba\x94 \xe5\xb2\x81 \xe7\x9a\x84 \xe6\x97\xb6\xe5\x80\x99 \xe5\xb0\xb1 \xe6\x98\xaf\xe6\x9c\x89 \xe5\x90\x8d \xe5\x85\xb6 \xe5\xb0\x91 \xe4\xba\x86'"
I want to convert string of bytes string into string so that i could use decode function to normal result.
First, put an r before it so that the \x keeps both characters. Then ast.literal_eval() will work.
import ast
string1 = r"b'\xe6\x88\x91\xe4\xbb\xac \xe7\xb4\xa2\xe8\xa6\x81 \xe6\x8e\xa8\xe5\xb9\xbf \xe7\x9a\x84 \xe6\x98\xaf\xe4\xb8\x80 \xe5\xbe\x97 \xe6\x96\xb9\xe6\x96\xb9 \xe6\x96\xb9\xe8\xa8\x80 \xe4\xb8\xba\xe5\x9f\xba \xe7\xa1\x80 \xe6\x96\xb9\xe8\xa8\x80 \xe4\xb8\x80 \xe5\x8c\x97\xe4\xba\xac \xe5\xb7\xb2 \xe5\x9b\xa0 \xe4\xb8\xba \xe6\xa0\x87\xe5\x87\x86 \xe7\x9a\x84 \xe6\x99\xae\xe9\x80\x9a \xe8\xaf\x9d \xe4\xbb\x96 \xe4\xbb\x8e \xe5\x84\xbf\xe7\xab\xa5 \xe6\x97\xb6\xe4\xbb\xa3 \xe8\xb5\xb7 \xe5\xb0\xb1 \xe5\x96\x9c\xe6\xac\xa2 \xe4\xb8\x8b \xe5\x9b\xb4\xe6\xa3\x8b \xe5\x9c\xa8 \xe5\x8d\x81\xe4\xba\x94 \xe5\xb2\x81 \xe7\x9a\x84 \xe6\x97\xb6\xe5\x80\x99 \xe5\xb0\xb1 \xe6\x98\xaf\xe6\x9c\x89 \xe5\x90\x8d \xe5\x85\xb6 \xe5\xb0\x91 \xe4\xba\x86'"
bytes1 = ast.literal_eval(string1)
print(bytes1.decode('utf8')) # 我们 索要 ...

Python find CRC32 of string

I tried to get crc32 of a string data type variable but getting the following error.
>>> message='hello world!'
>>> import binascii
>>> binascii.crc32(message)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
For a string values it can be done with binascii.crc32(b'hello world!') but I would like to know how to do this for a string data-type variable
When you are computing crc32 of some data, you need to know the exact value of bytes you are hashing. One string can represent different values of bytes in different encodings, therefore passing string as parameter is ambiguous.
When using binascii.crc32(b'hello world!'), you are converting char array into array of bytes using simple ascii table as conversion.
To convert any string, you can use:
import binascii
text = 'hello'
binascii.crc32(text.encode('utf8'))
This can be done using binascii.crc32 or zlib.crc32. This answer improves upon the prior answer by Tomas by documenting both modules and by producing a string output besides just an integer.
# Define data
> text = "hello"
> data = text.encode()
> data
b'hello'
# Using binascii
> import binascii
> crc32 = binascii.crc32(data)
> crc32
907060870
> hex(crc32)
'0x3610a686'
> f'{crc32:#010x}'
'0x3610a686'
# Using zlib
> import zlib
> zlib.crc32(data)
907060870 # Works the same as binascii.crc32.
If you don't want the string output to have the 0x prefix:
> import base64
> crc32 = 907060870
> digest = crc32.to_bytes(4, 'big')
> digest
b'6\x10\xa6\x86'
> base64.b16encode(digest).decode().lower()
'3610a686'

python from hex to shellcode format

I try to convert a hex string to shellcode format
For example: I have a file in hex string like aabbccddeeff11223344
and I want to convert that through python to show this exact format:
"\xaa\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44" including the quotes "".
My code is:
with open("file","r") as f:
a = f.read()
b = "\\x".join(a[i:i+2] for i in range(0, len(a), 2))
print b
so my output is aa\xbb\xcc\xdd\xee\xff\x11\x22\x33\x44\x.
I understand I can do it via sed command but I wonder how I may accomplish this through python.
The binascii standard module will help here:
import binascii
print repr(binascii.unhexlify("aabbccddeeff11223344"))
Output:
>>> print repr(binascii.unhexlify("aabbccddeeff11223344"))
'\xaa\xbb\xcc\xdd\xee\xff\x11"3D'

How to handle UTF8 string from Pythons imaplib

Python imaplib sometimes returns strings that looks like this:
=?utf-8?Q?Repertuar_wydarze=C5=84_z_woj._Dolno=C5=9Bl=C4=85skie?=
What is the name for this notation?
How can I decode (or should I say encode?) it to UTF8?
In short:
>>> from email.header import decode_header
>>> msg = decode_header('=?utf-8?Q?Repertuar_wydarze=C5=84_z_woj._Dolno=C5=9Bl=C4=85skie?=')[0][0].decode('utf-8')
>>> msg
'Repertuar wydarze\u0144 z woj. Dolno\u015bl\u0105skie'
My computer doesn't show the polish characters, but they should appear in yours (locales etc.)
Explained:
Use the email.header decoder:
>>> from email.header import decode_header
>>> value = decode_header('=?utf-8?Q?Repertuar_wydarze=C5=84_z_woj._Dolno=C5=9Bl=C4=85skie?=')
>>> value
[(b'Repertuar wydarze\xc5\x84 z woj. Dolno\xc5\x9bl\xc4\x85skie', 'utf-8')]
That will return a list with the decoded header, usually containing one tuple with the decoded message and the encoding detected (sometimes more than one pair).
>>> msg, encoding = decode_header('=?utf-8?Q?Repertuar_wydarze=C5=84_z_woj._Dolno=C5=9Bl=C4=85skie?=')[0]
>>> msg
b'Repertuar wydarze\xc5\x84 z woj. Dolno\xc5\x9bl\xc4\x85skie'
>>> encoding
'utf-8'
And finally, if you want msg as a normal utf-8 string, use the bytes decode method:
>>> msg = msg.decode('utf-8')
>>> msg
'Repertuar wydarze\u0144 z woj. Dolno\u015bl\u0105skie'
You can directly use the bytes decoder instead , here is an example:
result, data = imapSession.uid('search', None, "ALL") #search and return uids
latest_email_uid = data[0].split()[-1] #data[] is a list, using split() to separate them by space and getting the latest one by [-1]
result, data = imapSession.uid('fetch', latest_email_uid, '(BODY.PEEK[])')
raw_email = data[0][1].decode("utf-8") #using utf-8 decoder`

Python 3.3: Hex and Dec converting

I'm able to convert Hex data to Decimal with:
file = open("my_text.txt", "r+")
data = input("Type Hex: ")
hex = int(data, 16)
str(hex)
print(str(hex))
file.write(str(hex))
file.close()
input("close: ")
But how can I convert Decimal data, like a number or a sentence, to Hex? Also, is it possible to write data to a hexadecimal offset?
How about something like this?
>>> print(hex(257))
0x101
>>> for ch in b'abc':
... print(hex(ch))
...
0x61
0x62
0x63
BTW, assigning to a variable called "hex" occludes the built-in function - it's best to avoid that.
HTH

Categories

Resources