Python - decode('hex')
MRAB
python at mrabarnett.plus.com
Mon Feb 20 15:04:20 EST 2017
On 2017-02-20 19:43, Ganesh Pal wrote:
> On Feb 21, 2017 12:17 AM, "Rhodri James" <rhodri at kynesim.co.uk> wrote:
>
> 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
>
>
> Thanks for the reply James.
>
> How can I make my program 2 look like program 1 , any hacks ? because I
> get newdata value( as a hx return value Of type string )from a function.
>
In this:
newdata.decode('hex')
The .decode method doesn't change newdata in-place, it _returns_ the result.
You're doing anything with that result. You're not binding (assigning)
it to a name. You're not passing it into a function. You're not doing
_anything_ with it. You're just letting it be discarded, thrown away.
You could ask the user for the hex string:
hex_string = raw_input('Enter the hex string: ')
and then decode it:
newdata = hex_string.decode('hex')
More information about the Python-list
mailing list