Python's from_bytes, To_bytes__python

Source: Internet
Author: User

First, let's take a look at two __builtin__ functions

NUM1 = Int.from_bytes (b ', byteorder = ' big ')
num2 = int.from_bytes (b ', byteorder = ' little ')
print (' (%s, '% ' Num1 ', Num1, '), ', ' (%s, '% ' num2 ', num2, ') ')
Result: (NUM1, 12594), (num2, 12849)
BYT1 = (1024). To_bytes (2, Byteorder = ' big ')
byt2 = (1024). To_bytes (Ten, Byteorder = ' big ')
byt3 = ( -1024). to_bytes (byteorder= ' big ')
Lis1 = [' byt1 ', ' byt2 ', ' byt3 ', ' byt4 ']
lis2 = [Byt1, Byt2, Byt3, byt4]
lis3 = Zip (lis1, lis2)
dic = Dict (lis3 )
Print (DIC)
Result
Byt1 ': B ' \x04\x00 '
Byt2 ': B ' \x00\x04\x00\x00\x00\x00\x00\x00\x00\x00 '
Byt3 ': B ' \xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00 '
Byt4 ': B ' \xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00 ' int.from_bytes () function is to convert bytes to int-type number ' 12 ' if not labeled as ASCII value, ' 1 ' = 49 = 0011 0001, ' 2 ' = 50 = 0011 0010, if Byteorder = ' big ', B ' 12 ' = 0010 0001 0010 0010 = 12594; if Byteorder = ' Littlele ', b ' 12 ' = 0011 0010 0011 0001 = 12849. The third parameter is signed representing signed and unsigned, (number). To_bytes () function converts integers to byte
(1024). to_bytes (Byteorder = ' big '), an int, 4 bytes. 1024 = 0000 0000 0000 0000 0000 0100 0000 0000, because the given is 10, so gather 10 bytes, the high level with 6 0000 0000
Occupy, if the last in the 16, 1024 = B ' \x00\x00\x00\ x00\x00\x00\x00\x00x04\x00
Looking at an example:
byt3 = ( -1024). To_bytes (Ten, byteorder= ' big ', signed = ' true '), due to signed = ' true ',-1024 = 1000 ... (one) 0000 0000 0000  0000 0000 0100 0000 0000, Symbol bit 1, ... Omitted
11 0000, because the negative numbers are represented by the complement, so first to 1024 of the inverse code, that is, the symbol bit unchanged, the other bit 0 variable 1,1 to 0, get: 1111 ... (11) 1111 1111 1111 1111 1111 1011 1111 1111, the inverse code + 1, get the complement:
1111 ... (11) 1111 1111 1111 1111 1111 1100 0000 0000, in 16 notation: \xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00
Another example:
NUM3 = int.from_bytes (b ' \xf3\x25 ', byteorder = ' little ')
F3 = 243 (10) = 1111 0011,25 = 37 (10) = 0010 0101,byteorder = ' little ', byte low occupies the main role, get: 0010 0101 1111 0011, get decimal: 9715
NUM3 = int.from_bytes (b ' \xf3\x25 ', Byteorder = ' big ', signed = ' true ')
F3 = 243 (10 in) = 1111 0011,25 = 37 (10) = 0010 0101,byteorder = ' big ', the high position of the byte occupies the main function, obtains: 1111 0011 The 0010 = ' true ', description has
the character number, and the high position is 1, therefore uses the complement: 1000 1100 1101 1011 namely:-3291

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.