Advertisement
bojjenclon

BinaryConverter.pyw

May 18th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. import sys, os
  2. from PyQt4 import Qt, QtGui, QtCore
  3. import binascii
  4. import re
  5.  
  6. class MainWindow( QtGui.QMainWindow ):
  7.     width = 640
  8.     height = 480
  9.    
  10.     def __init__( self ):
  11.         QtGui.QMainWindow.__init__( self )
  12.  
  13.         self.setWindowTitle( "Binary & Ascii Converter" )
  14.        
  15.         main = QtGui.QWidget()
  16.  
  17.         vbox = QtGui.QVBoxLayout()
  18.         hbox = QtGui.QHBoxLayout()
  19.        
  20.         self.text = QtGui.QTextEdit( self )
  21.        
  22.         self.label = QtGui.QTextEdit( self )
  23.         self.label.setReadOnly( True )
  24.         self.label.setStyleSheet( "background-color: rgb( 0, 0, 0 ); color: rgb( 255, 255, 255 );" )
  25.        
  26.         btnBinary = QtGui.QPushButton( 'To Binary', self )
  27.         btnAscii = QtGui.QPushButton( 'To Ascii', self )
  28.        
  29.         hbox.addWidget( btnBinary )
  30.         hbox.addWidget( btnAscii )
  31.        
  32.         vbox.addWidget( self.text )
  33.         vbox.addWidget( self.label )
  34.         vbox.addLayout( hbox )
  35.        
  36.         main.setLayout( vbox )
  37.  
  38.         self.setCentralWidget( main )
  39.  
  40.         self.resize( MainWindow.width, MainWindow.height )
  41.         self.center()
  42.        
  43.         self.statusBar().showMessage( 'Ready' )
  44.        
  45.         self.connect( btnBinary, QtCore.SIGNAL( 'clicked()' ), self.toBinary )
  46.         self.connect( btnAscii, QtCore.SIGNAL( 'clicked()' ), self.toAscii )
  47.        
  48.     def toBinaryHardWay( self ):
  49.         out = ""
  50.        
  51.         b = bytearray( str( self.text.toPlainText() ) )
  52.        
  53.         for i in b:
  54.             val = i
  55.            
  56.             for ii in xrange( 8 ):
  57.                 out += "0" if ( val & 128 ) == 0 else "1"
  58.                 val <<= 1
  59.                
  60.             out += " "
  61.            
  62.         print out
  63.        
  64.     def toBinary( self ):
  65.         try:
  66.             out = bin( int( binascii.hexlify( str( self.text.toPlainText() ) ), 16 ) ).replace( "b", "" )
  67.             out = re.sub( "(.{8})", "\\1\n", out, re.DOTALL )
  68.            
  69.             self.statusBar().showMessage( 'Ascii data succesfully converted to binary' )
  70.            
  71.             self.label.setText( out )
  72.         except:
  73.             self.statusBar().showMessage( 'Error: Invalid data entered, please ensure that the information is correct and try again' )
  74.            
  75.             self.label.setText( self.text.toPlainText() )
  76.        
  77.     def toAscii( self ):
  78.         try:
  79.             n = int( str( self.text.toPlainText().replace( "\n", "" ) ), 2 )
  80.             out = binascii.unhexlify( '%x' % n )
  81.            
  82.             self.statusBar().showMessage( 'Binary data succesfully converted to ascii' )
  83.            
  84.             self.label.setText( out )
  85.         except:
  86.             self.statusBar().showMessage( 'Error: Data is not binary, please check your input and try again' )
  87.            
  88.             self.label.setText( self.text.toPlainText() )
  89.    
  90.     def center( self ):
  91.         screen = QtGui.QDesktopWidget().screenGeometry()
  92.         size = self.geometry()
  93.  
  94.         widthDif = screen.width() - size.width()
  95.         heightDif = screen.height() - size.height()
  96.  
  97.         x = widthDif / 2
  98.         y = heightDif / 2
  99.  
  100.         self.move( x, y )
  101.        
  102. def main():
  103.     app = QtGui.QApplication( sys.argv );
  104.  
  105.     window = MainWindow();
  106.     window.show();
  107.  
  108.     sys.exit( app.exec_() )
  109.  
  110. if __name__ == '__main__':
  111.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement