>>> x = bytearray(b'\n\x91\x8c\xbc\xd4\xc6\xd2\x19\x98\x14x\x0f1q!\xdc|C\xae\xe0
\xdc\xf1\xf1')
>>> import binascii
>>> print(binascii.hexlify(x))
0a918cbcd4c6d2199814780f317121dc7c43aee0dcf1f1
Use binascii if you want all of it to be printed as a hex string
Answer from Lelouch Lamperouge on Stack Overflow>>> x = bytearray(b'\n\x91\x8c\xbc\xd4\xc6\xd2\x19\x98\x14x\x0f1q!\xdc|C\xae\xe0
\xdc\xf1\xf1')
>>> import binascii
>>> print(binascii.hexlify(x))
0a918cbcd4c6d2199814780f317121dc7c43aee0dcf1f1
Use binascii if you want all of it to be printed as a hex string
Use bytes.hex()
>>> x = bytearray([0x01,0x02,0xff])
>>> print(x.hex())
0102ff
How to print python Byte variable? - Stack Overflow
python - How to do pprint in a byte array type? - Stack Overflow
bytes but print a string!
Coming from C, how to better understand Python's byte data type?
Addressing the above "too small a task to require a library" issue by a straightforward implementation (using f-strings, so Python 3.6+):
def sizeof_fmt(num, suffix="B"):
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return f"{num:.1f}Yi{suffix}"
Supports:
- all currently known binary prefixes
- negative and positive numbers
- numbers larger than 1000 Yobibytes
- arbitrary units (maybe you like to count in Gibibits!)
Example:
>>> sizeof_fmt(168963795964)
'157.4GiB'
by Fred Cirera
A library that has all the functionality that it seems you're looking for is humanize. humanize.naturalsize() seems to do everything you're looking for.
Example code (Python 3.10)
import humanize
disk_sizes_list = [1, 100, 999, 1000,1024, 2000,2048, 3000, 9999, 10000, 2048000000, 9990000000, 9000000000000000000000]
for size in disk_sizes_list:
natural_size = humanize.naturalsize(size)
binary_size = humanize.naturalsize(size, binary=True)
print(f" {natural_size} \t| {binary_size}\t|{size}")
Output
1 Byte | 1 Byte |1
100 Bytes | 100 Bytes |100
999 Bytes | 999 Bytes |999
1.0 kB | 1000 Bytes |1000
1.0 kB | 1.0 KiB |1024
2.0 kB | 2.0 KiB |2000
2.0 kB | 2.0 KiB |2048
3.0 kB | 2.9 KiB |3000
10.0 kB | 9.8 KiB |9999
10.0 kB | 9.8 KiB |10000
2.0 GB | 1.9 GiB |2048000000
10.0 GB | 9.3 GiB |9990000000
9.0 ZB | 7.6 ZiB |9000000000000000000000
» pip install humanfriendly
Im learning crypto ctf with python and there is something that really can't figure out on my own
i have a flag encrypted with XOR with a key. they are represented as hex. after convert it to bytes, i xor them and then use it Crypto.Util.number.long_to_bytes() to find the flag.
before the XOR operation, the bytes values is like "\xa6\xc8\xb6s<\x9b"\xde{\xc0%2f\xa3\x86}\xf5Z\xcd\xe8c^\x19\xc73\x13"
after i removed the key that was XOR with the flag, the bytes values of flag is
crypto{x0r_i5_ass0c1at1v3}
i checked the var with type() and it's a bytes not a str. How come that before that its bytes values has hex values and then only normale characters? Or maybe those arent hex?
» pip install byte-formatter
I'm reading the official docs and all, tried some examples, but still feels like it's just a string.
Then I didn't understand why when I loop through a bytes object and print the current element it comes out as an integer, but when I print the whole content of the object it comes out as string like: b'whatever is in the string', and when I cast something using bytes() it returns a string formatted as hex.