Python frequently uses conversions between decimal, 16, String, and byte strings (long-term update posts)

Source: Internet
Author: User
Tags binary to decimal ord pack unpack

When the protocol is resolved. Always encounter a variety of data conversion problems, from binary to Decimal, from byte string to integer, etc.

Not much nonsense on. Directly on the sample


Binary conversions between integers:

    • 10-in-turn 16-in: Hex (==>) 0x10
    • 16-in-turn 10-in: Int (' 0x10 ', +) ==> 16
Also similar to the OCT (), Bin ()


-------------------


String to Integer:
    • 10 binary string: Int (' ten ') ==> 10
    • 16 binary string: Int (' ten ', +) ==> 16
    • 16 binary string: Int (' 0x10 ', +) ==> 16


-------------------


byte string to Integer:
    • Escaped to short integer: Struct.unpack ('
    • Escaped as Long integer: Struct.unpack (' <l ', bytes (b ' \x01\x00\x00\x00 ')) ==> (1,)


-------------------


Integer to byte string:
    • Convert to two bytes: Struct.pack ('
    • Convert to four bytes: Struct.pack (' <ll ', up) ==> B ' \x01\x00\x00\x00\x02\x00\x00\x00 '


-------------------


String-To-byte string:
    • String encoded as bytecode: ' 12abc '. Encode (' ASCII ') ==> B ' 12abc '
    • Array of numbers or characters: bytes ([1 '), Ord (' 2 ')]) ==> B ' \x01\x0212 '
    • 16 binary string: bytes (). Fromhex (' 010210 ') ==> B ' \x01\x02\x10 '
    • 16 Binary strings: bytes (map (ord, ' \x01\x02\x31\x32 ')) ==> B ' \x01\x0212 '
    • 16 binary arrays: bytes ([0x01,0x02,0x31,0x32]) ==> B ' \x01\x0212 '


-------------------


BYTE string:
    • Byte code decoded to a string: bytes (b ' \x31\x32\x61\x62 '). Decode (' ASCII ') ==> 12ab
    • byte string to 16 binary representation, entrained ascii:str (bytes (b ' \x01\x0212 ')) [2:-1] ==> \x01\x0212
    • byte string to 16 binary representation, fixed two characters: Str (binascii.b2a_hex (b ' \x01\x0212 ')) [2:-1] ==> 01023132
    • byte string to 16 binary array: [Hex (x) for x in bytes (b ' \x01\x0212 ')] ==> [' 0x1 ', ' 0x2 ', ' 0x31 ', ' 0x32 ']


===================


Test the Python source code

' Created on August 21, 2014 @author:lenovo ' ' Import binasciiimport structdef example (Express, Result=none): if result = = N One:result = eval (Express) print (Express, ' ==> ', result) if __name__ = = ' __main__ ': print (' binary conversion between integers ') : ') print ("10 binary to 16", end= ': '); Example ("Hex ()") print ("16 to 10", end= ': '), Example ("Int (' 0x10 ')") print ("similar to Oct (), Bin ()") print (' \ n-------------------\ n ') print (' String to Integer: ') print ("10 binary string", end= ":"); Exampl E ("Int (')") print ("16 binary string", end= ":"), Example ("Int (') ',") ") print (" 16 binary string ", end=": "); example (" int "(' 0x10 ', 1 6) print (' \ n-------------------\ n ') print (' byte string-to-integer: ') print ("Escaped to short integer", end= ":"); Example (r "struct.un Pack (' 

But here, there's a better, simpler way, welcome.


The above principles are relatively simple, look at the clear.

But here, there's a better, simpler way to welcome

Python frequently uses conversions between decimal, 16, String, and byte strings (long-term update posts)

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.