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

Source: Internet
Author: User
Tags binary to decimal unpack

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

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 ('
  • Escape as a long integer: struct. unpack ('<l', bytes (B' \ x01 \ x00 \ x00 \ x00') => (1 ,)


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


Integer to byte string:
  • Convert to two bytes: struct. pack ('<HH', 1, 2) => B '\ x01 \ x00 \ x02 \ x00'
  • To four bytes: struct. pack ('<LL', 1, 2) => 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 Number: ') print ("10 hexadecimal string", end = ":"); example ("int ('10')") print ("16 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.


Python converts a hexadecimal string to a hexadecimal number, for example, '0x0012e', to 0x0012e.

A = '0x0012e'

B = hex (eval ())
Print B

Output

0x12e

Note: Generally, when the computer outputs The hexadecimal number directly, it does not add 0. Therefore
0x12e is 0x0012e, as if 0005 and 5 are stored in the same integer value.

How to convert decimal to hexadecimal in python

The variables in hex brackets can only be digits in decimal or octal format and cannot be strings.



 

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.