need help understanding: converting text to binary

MRAB python at mrabarnett.plus.com
Mon Apr 22 21:31:57 EDT 2019


On 2019-04-23 01:54, Eli the Bearded wrote:
> Here's some code I wrote today:
> 
[snip]
> 
> # decode a single hex digit
> def hord(c):
>      c = ord(c)
>      if c >= ord(b'a'):
>          return c - ord(b'a') + 10
>      elif c >= ord(b'A'):

There's a bug here:

>          return c - ord(b'a') + 10

It should be:

          return c - ord(b'A') + 10

>      else:
>          return c - ord(b'0')
> 
However, the entire function can be replaced with int(c, 16), anyway. :-)

[snip]



More information about the Python-list mailing list