Home > database >  hexdump function in python
hexdump function in python

Time:01-06

With below python code

zero = '00000000000000000000000000000000'
print(bytes.fromhex(zero))
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

three = '33333333333333333333333333333333'
print(bytes.fromhex(three))
b'3333333333333333

On printing bytes.fromhex(zero), exact 32 character is printed in hexadecimal.

But while printing bytes.fromhex(three), only 16 character is printed. zero and three string both are of same length:32

CodePudding user response:

The method fromhex(string) returns a bytes object, decoding the given string object. The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.

It means the method read one-byte hex string "33" as ASCII code and 33 in ASCII code represents number 3.

Try "34343434" or "32323232". You will see 4444 or 2222.

Have a look at https://docs.python.org/3/library/stdtypes.html

  •  Tags:  
  • Related