Python - decode('hex')

Rhodri James rhodri at kynesim.co.uk
Mon Feb 20 13:30:48 EST 2017


On 20/02/17 17:55, Ganesh Pal wrote:
> 1. The only difference between both the programs  the difference are just
> the below lines.
>
> newdata = '64000101057804'.decode('hex')
>
>         and
>
> newdata = ""
> newdata = '64000101057804'
> newdata.decode('hex')
>
>
> What is happening here and how do I fix this in  program 2  ?   for my eyes
> there doesn't look any difference .

Python strings are immutable; methods like decode() create a brand new 
string for you.  What your program 2 version does is to name the string 
of hex digits "newdata", decode it as hex into a new string and then 
throw that new string away.  Your program 1 version by contrast decodes 
the string of digits as hex and then names is "newdata", throwing the 
original string of digits away.

> question 2:
>
> I am using  the variable  newdata  because  I can't hardcore the  value , I
> have to  keep changing this every time the function is called,  will return
> it as a string help me slove this problem
>
> def get_data() :
>       return str(data)
>
> new_data =get_data(input)

That seems like a lot of work and syntax errors to do

new_data = raw_input()

(or new_data = input() if you're writing Python 3, which you weren't)

Since you probably want to do this for your file name too, you might 
want to pass them as command line parameters to the script.  Look up 
sys.argv[] in the standard library, or the argparse module if you're 
feeling keen.

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list