# Python common decimal, 16 binary, String, byte string conversion between

Source: Internet
Author: User

*************************************************************************************************************** ****************

Key points: 1, then python in the bottom of the byte are encoded, Python2 is the Assic code, Python3 is Unicode, are bytes, not binary, encoding and binary conversion between the bottom of the python implementation of the function

2, and then write a Python program, are not related to binary, Socet network transmission is the middle of the byte refers to the encoding

What does the 3,b "mean?"

*************************************************************************************************************** ****************

When parsing a protocol, you will always encounter a variety of data conversion problems, from binary to Decimal, from byte string to integer, etc.

Not much nonsense on that directly on the example

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 ']

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

Python Source for testing

[Python] View plain copy

‘‘ ‘‘ ‘‘
Created on August 21, 2014

@author: lenovo
‘‘ ‘
import binascii
import struct

def example (express, result = None):
if result == None:
result = eval (express)
print (express, ‘==>‘, result)

if __name__ == ‘__main__’:

print ('Basic conversion between integers:')
print ("Decimal to Hexadecimal", end = ‘:‘); example ("hex (16)")
print ("Hexadecimal to decimal", end = ‘:‘); example ("int (‘ 0x10 ‘, 16)")
print ("Similar to oct (), bin ()")

print (‘\ n ------------------- \ n‘)

print ('String to integer:')
print ("Decimal string", end = ":"); example ("int (‘10‘) ")
print ("hexadecimal string", end = ":"); example ("int (‘10‘, 16) ")
print ("hexadecimal string", end = ":"); example ("int (‘ 0x10 ‘, 16)")

print (‘\ n ------------------- \ n‘)

print ('Byte string to integer:')
print ("escape to short integer", end = ":"); example (r "struct.unpack (‘ <hh ’, bytes (b‘ \ x01 \ x00 \ x00 \ x00 ‘))))
print ("escape to long integer", end = ":"); example (r "struct.unpack (‘ <L ’, bytes (b‘ \ x01 \ x00 \ x00 \ x00 ‘))))

print (‘\ n ------------------- \ n‘)

print ('Integer to byte string:')
print ("Convert to two bytes", end = ":"); example ("struct.pack (‘ <HH ‘, 1,2)")
print ("Convert to four bytes", end = ":"); example ("struct.pack (‘ <LL ‘, 1,2)")

print (‘\ n ------------------- \ n‘)

print (‘String to byte string:’)
print (‘String encoded as bytecode’, end = ":"); example (r "‘ 12abc‘.encode (‘ascii‘) ")
print (‘Number or character array’, end = ":"); example (r "bytes ([1,2, ord (‘ 1 ‘), ord (‘ 2 ‘)])")
print (‘hexadecimal string’, end = ‘:‘); example (r "bytes (). fromhex (‘ 010210 ‘)")
print (‘hexadecimal string’, end = ‘:‘); example (r "bytes (map (ord,‘ \ x01 \ x02 \ x31 \ x32 ‘)))
print (‘Hexadecimal array’, end = ’:‘); example (r’bytes ([0x01,0x02,0x31,0x32]) ‘)

print (‘\ n ------------------- \ n‘)

print ('Byte string to string:')
print (‘Bytecode decoded to string’, end = ":"); example (r "bytes (b‘ \ x31 \ x32 \ x61 \ x62 ‘). decode (‘ ascii ’)")
print (‘byte string to hexadecimal representation, with ascii’, end = ":"); example (r "str (bytes (b‘ \ x01 \ x0212 ‘)) [2: -1]")
print ('Byte string to hexadecimal representation, fixed two character representation', end = ":"); example (r "str (binascii.b2a_hex (b '\ x01 \ x0212')) [2: -1 ] ")
print (‘Byte string to hexadecimal array’, end = ":"); example (r "[hex (x) for x in bytes (b‘ \ x01 \ x0212 ‘)]")

print (‘\ n ==================== \ n‘)
print ("The above principles are relatively simple, you can understand it by taking a look. Here is just a way to introduce new ideas. There are better and easier ways, welcome and welcome")

Python common decimal, 16 binary, String, byte string conversion between

Related Keywords:
Related Article

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.