简体   繁体   中英

Get byte size of the binary data (python3.6)

I have a string which I converted to binary representation using str.encode('utf-8') . After that I expect getsizeof() and len() return the same value, but it appears that sys.getsizeof() always return a bigger value.

I then send this binary data over socket to node.js server and store them in Buffer. Both Buffer.length and Buffer.byteLength return the same value, which is equal to len() value in Python.

I can't figure out what is going on there and why Buffer.byteLength is not the same as sys.getsizeof() .

My data is not always strings or may have different encoding, so I want to make sure that I know the size in bytes, not in characters.

sys.getsizeof returns the size that the object takes in memory. That includes all additional data which Python needs in order to work with the object (eg the information that this is a string in the first place and not an integer).

For example, see that it has a certain size for an empty string:

>>> sys.getsizeof('')
49

and even for None :

>>> sys.getsizeof(None)
16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM