Friday, 25 April 2014

Hexadecimal encoder & decoder in Python

Best thing , I love about Python is, It has lots of inbuilt Pre-defined functions . which shorten our code by a large extend .


Hexadecimal encoding & decoding is very easy & need only few lines of  in python . 

Here Is Python2.7 code for Hex conversion from string ,and from Hex to string 


  1. ##http://beginer2cs.blogspot.com
  2. ### Hexadecimal Encoder & decoder
  3. #NOTE It Convert It treat Number as string
  4. def Hex_encode(a):
  5.     '''
  6.     take string or integer as input
  7.     &
  8.     return hexadecimal encoded of it
  9.    '''
  10.     b=a.encode("hex")
  11.     return b
  12. def Hex_decode(a):
  13.     '''
  14.    Take Hexadecimal encode
  15.    &
  16.    return actual value
  17.    '''
  18.     b=a.decode("hex")
  19.     return b
  20.    
  21. = raw_input('Enter data to Encode in Hexadecimal : ') #take input
  22. print "\nHex_Encoded data is :",Hex_encode(a),"\n"
  23. = raw_input('Enter Encoded data to decode : ') #take input
  24. print "\nHex_decoded data is : ",Hex_decode(c)

Output for this simple code is :
I generally used a basic shift cipher  then hex encryption to to make some special comment at Facebook .such that only I and some of my  friend can understand it ..

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets