Use Python's built-in modules and functions to convert the number of different binary numbers

Source: Internet
Author: User
BINASCII module:
It contains a function that converts a binary value into a 16 binary, which can also be reversed. #binary_value是二进制数值不是字符串, not 1010 of the int type.

Binascii.b2a_hex (binary_value) # #binary_value general read binary file can get  >> ' 89 ' 
 
   
 
  

Python comes with the BUILTIN function:
Bin (num) decimal value = = = "Binary string"

Bin (Ten)  >> ' 0b1010 ' 
 
   
 
  

Oct (num) decimal value = = = "Octal string"

Oct (Ten)  >> ' 012 ' 
 
   
 
  

Hex (num) decimal value = = = "Hex string"

Hex (  >> ' 0x14 ') 
 
   
 
  

Int (str, base) Other binary string = = = "Decimal value, where base represents STR specifically belongs to which binary, if 2 means str is binary, default base is decimal

Int (' 2 ') >>20 int (' 20 ', 
 
   
  
   
    
    8) >>8 
   
    
     
     int (' + ', ' + ') ' (' + ') ', ' >>2 int ', >>20 
    
     
      
      int (' + ', +) >>32 
     
       
     
      
    
     
   
    
  
   
 
  

Character and number conversion functions:
chr (int) integer-to-character

Chr  >> ' A ', 
 
   
 
  

Ord (CHR) character turn-integer

Ord (' a ')  >>97, 
 
   
 
  

Finally, give an example of reading a picture file binary content:

#!/usr/bin/env python  #encoding: utf-8 import Binascii   

1010 Brush screen effect, is not a bit of the hacker Empire Bright Ah, hehe

  • 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.