byte strings python

Solutions on MaxInterview for byte strings python by the best coders in the world

showing results for - "byte strings python"
Claude
20 Sep 2018
1It is a common misconception that text is ascii or utf8 or cp1252, and therefore bytes are text.
2Text is only text, in the way that images are only images. 
3The matter of storing text or images to disk is a matter of encoding that data into a sequence of bytes. 
4There are many ways to encode images into bytes: Jpeg, png, svg, and likewise many ways to encode text, ascii, utf8 or cp1252.
5Once encoding has happened, bytes are just bytes. 
6Bytes are not images anymore,they have forgotten the colors they mean; 
7although an image format decoder can recover that information.
8Bytes have similarly forgotten the letters they used to be. 
9In fact, bytes don't remember wether they were images or text at all. Only out of band knowledge 
10(filename, media headers, etcetera) can guess what those bytes should mean, and even that can be wrong (in case of data corruption)
11so, in python (py3), we have two types for things that might otherwise look similar; 
12For text, we have str, which knows it's text; it knows which letters it's supposed to mean. 
13It doesn't know which bytes that might be, since letters are not bytes. 
14We also have bytestring, which doesn't know if it's text or images or any other kind of data.
15The two types are superficially similar, since they are both sequences of things, but the things that they are sequences of is quite different.
16Implementationally, str is stored in memory as UCS-? where the ? is implementation defined, 
17it may be UCS4, UCS2 or UCS1, depending on compile time options and which codepoints are present in the represented string.