bytes of data types

Python3 most important new features probably be the text and binary data made a clearer distinction. Text always Unicode, represented by str type binary data type represented by bytes. Str Python3 not mix any bytes and implicit manner, which is particularly clear that the distinction between the two. You can not concatenate strings and byte packets, bytes can not be in the bag search string (and vice versa), nor can the string passed to the function parameter byte packets (and vice versa). This is a good thing.

 

, Bytes type of character string is treated as a unit in bytes processed.

Even the built-in type sequence of bytes of data objects the method and the same basic string data type, is immutable and used in all operations.

Only bytes object is responsible for recording the recording target in the desired binary sequence of bytes, in the end as to represent the object (such as what is in the end character) by decoding the encoded formats determined. Python3 in, bytes usually used for network data transmission, saving files and binary images and so on. Can be generated by calling bytes () bytes example, the value in the form of b'xxxxx ', where' xxxxx 'is a hexadecimal escape to the plurality of strings (in the form of a single x is: \x12where \ x is lowercase hexadecimal escape character, 12 for the two hexadecimal digits) sequences, each of which represents a hexadecimal number of bytes (eight binary number in the range 0-255), for the same If the string different coding bytes generated objects, it will form a different value.

 

 

In any case, the boundaries between strings and byte packet is inevitable, the following illustration is very important, be sure to keep in mind:

 

b = b '' # Create an empty bytes
B = byte () # Create an empty bytes
B = b'hello '# hello directly specify this type is bytes
b = bytes (' string ', encoding =' coding type ' ) using the built-in bytes # methods, converts the string to the specified coded bytes
B = str.encode ( 'coding type') using a # character string encoded into bytes encode method, default utf-8 type

bytes.decode ( 'coding type '): the object decoding bytes into a string, utf-8 default decoding.

 

For bytes, as long as we know in some cases mandatory to use the Python3, and mutual conversion between it and the string type, other basic copy string.

Simple easy mode:

string = b'xxxxxx'.decode() Directly to the default codec utf-8 string into bytes

b = string.encode() Directly to the default utf-8 encoded string of bytes

 

Why computer memory address and hexadecimal?

Why hexadecimal

1, the computer hardware is 0101 binary, hexadecimal exactly a multiple of 2, easier to express a command or data. Hex shorter, since conversion time may be a hexadecimal top four binary numbers, i.e. a byte (8 binary can use two hexadecimal)

2, the first provisions of the ASCII character set used is 8bit (post-expansion, but the base unit or 8bit), 8bit with two hex can be directly expressed, regardless of reading or storing binary to be convenient than the other
3 computers CPU computing is in compliance with the ASCII character set, in such a way 16,32,64 in the development, therefore when hexadecimal data exchange also appears better
4, for a standardized, CPU, memory, hard drives we saw all calculated using the hexadecimal


Where hex used
to express the 1, network programming, when the need for exchange of data bytes is a byte parsing a byte process, a two byte hexadecimal can 0xFF. Capture the network can see through hexadecimal data transmission.
2, data is stored, is stored in the 0101 hardware, the storage of the expression system are byte manner

3, define some common values, such as: html we often use the expression of color, is used in hexadecimal, 4 bits can be expressed in hexadecimal millions of color information.

Guess you like

Origin www.cnblogs.com/fan-1994716/p/11654327.html