Re: need help understanding: converting text to binary

2019-04-23 Thread Eli the Bearded
In comp.lang.python, Cameron Simpson wrote: > On 23Apr2019 20:35, Eli the Bearded <*@eli.users.panix.com> wrote: >> That feels entirely wrong. I don't know what b'\x9A' means without >> knowing the character set and character encoding. If the encoding is a >> multibyte one, b'\x9A' doesn't mean a

Re: need help understanding: converting text to binary

2019-04-23 Thread Gregory Ewing
Cameron Simpson wrote: If you don't know the encoding then you don't know you're looking at a hex digit. OTOH, if the binary data contain ASCII data then you do know the encoding: it is ASCII. Not necessarily, it could be a superset of ASCII such as latin-1 or utf-8. You do need to know that

Re: need help understanding: converting text to binary

2019-04-23 Thread Cameron Simpson
On 23Apr2019 20:35, Eli the Bearded <*@eli.users.panix.com> wrote: In comp.lang.python, Chris Angelico wrote: Is there a more python-esque way to convert what should be plain ascii What does "plain ASCII" actually mean, though? ASCII encoded binary data. ASCII is code points that fit in 7-b

Re: need help understanding: converting text to binary

2019-04-23 Thread Eli the Bearded
In comp.lang.python, Paul Rubin wrote: > Eli the Bearded <*@eli.users.panix.com> writes: >> # decode a single hex digit >> def hord(c): ... > >def hord(c): return int(c, 16) That's a good method, thanks. > > # decode quoted printable, specifically the MIME-encoded words > > # variant which

Re: need help understanding: converting text to binary

2019-04-23 Thread Eli the Bearded
In comp.lang.python, Chris Angelico wrote: > Have you checked to see if Python can already do this? You mention I'm sure there's a library already. I'm trying to mix library usage with my own code to get practice writing in python. In this case, I want code to deal with MIME encoding in email he

Re: need help understanding: converting text to binary

2019-04-22 Thread MRAB
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 shou

Re: need help understanding: converting text to binary

2019-04-22 Thread Chris Angelico
On Tue, Apr 23, 2019 at 10:58 AM Eli the Bearded <*@eli.users.panix.com> wrote: > > Here's some code I wrote today: > > -- cut here 8< -- > HEXCHARS = (b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', > b'A', b'B', b'C', b'D', b'E', b'F', > b'a', b'b', b'c

need help understanding: converting text to binary

2019-04-22 Thread Eli the Bearded
Here's some code I wrote today: -- cut here 8< -- HEXCHARS = (b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F', b'a', b'b', b'c', b'd', b'e', b'f') # decode a single hex digit def hord(c): c = ord(c) if c >= or