Conversion between commonly used decimal, hexadecimal, string, and byte string in python (updated for a long time)

Source: Internet
Author: User
During protocol parsing, you will always encounter various data conversion problems. here we detail the hexadecimal conversion between integers and the conversion between integers, strings, and byte strings. Of course, it is just a matter of discussion. there are better and simpler methods. welcome.

During protocol parsing, you will always encounter various data conversion problems, from binary to decimal, from byte string to integer, etc.

Not much nonsense. let's look at the example directly.


Hexadecimal conversion between integers:

  • 10 to 16: hex (16) => 0x10
  • Hexadecimal to hexadecimal: int ('0x10', 16) => 16
Similarly, oct (), bin ()


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


String to integer:
  • 10-digit string: int ('10') => 10
  • Hexadecimal string: int ('10', 16) => 16
  • Hexadecimal string: int ('0x10', 16) => 16


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


Byte string to integer:
  • Escape as a short integer: struct. unpack (' (1, 0)
  • Escape as long integer: struct. unpack (' (1 ,)


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


Integer to byte string:
  • Convert to two bytes: struct. pack (' B '\ x01 \ x00 \ x02 \ x00'
  • To four bytes: struct. pack (' B '\ x01 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00'


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


String to byte string:
  • The string is encoded as a bytecode: '12abc'. encode ('ascii ') ==> B '12abc'
  • Number or character array: bytes ([1, 2, ord ('1'), ord ('2')]) => B '\ x01 \ x0212'
  • Hexadecimal string: bytes (). fromhex ('000000') ==> B '\ x01 \ x02 \ x10'
  • Hexadecimal string: bytes (map (ord, '\ x01 \ x02 \ x31 \ x32') => B' \ x01 \ x0212'
  • Hexadecimal array: bytes ([0x01,0x02,0x31,0x32]) ==> B '\ x01 \ x0212'


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


Byte string to string:
  • Bytecode is decoded as a string: bytes (B '\ x31 \ x32 \ x61 \ x62'). decode ('ascii ') ==> 12ab
  • Byte string to hexadecimal notation, with ascii: str (bytes (B '\ x01 \ x0212') [2:-1] ==>\ x01 \ x0212
  • Byte string to hexadecimal notation, fixed two character representation: str (binascii. b2a_hex (B '\ x01 \ x0212') [2:-1] => 01023132
  • Byte string to hexadecimal array: [hex (x) for x in bytes (B '\ x01 \ x0212')] ==> ['0x1', '0x2 ', '0x31', '0x32']


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


Python source code for testing

'''Created on August 21, 2014 @ author: lenovo ''' import binasciiimport structdef example (express, result = None): if result = None: result = eval (express) print (express, '=>', result) if _ name _ = '_ main _': print ('hexadecimal conversion between integers :') print ("Hexadecimal to hexadecimal", end = ':'); example ("hex (16)") print ("Hexadecimal to hexadecimal ", end = ':'); example ("int ('0x10', 16)") print ("similar to oct (), bin ()") print ('\ n ----------------- \ n') print ('string to integer:') print ("10 hexadecimal 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 type integer", end = ":"); example (r "struct. unpack ('
 
  
The above principles are relatively simple. you can see it at a glance. Here is just a reference, and there are better and simpler methods. welcome.

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.