Concatenating a Hash to a String

Dieter Maurer dieter at handshake.de
Tue Dec 1 13:32:00 EST 2020


"Ivan \"Rambius\" Ivanov" wrote at 2020-12-1 00:32 -0500:
> ...
>This code throws
>
>$ ./hashinstr.py
>Traceback (most recent call last):
>  File "./hashinstr.py", line 16, in <module>
>    gen_sql(s)
>  File "./hashinstr.py", line 13, in gen_sql
>    sql = "insert into HASHES value ('" + ehash + "')"
>TypeError: can only concatenate str (not "bytes") to str

With Python 3, you must carefully distinquish `bytes` (a
sequence of numbers between 0 and 255) and `str` (a sequence of
(unicode) characters).
`base64` operates on `bytes`; `"..."` constructs an `str`;
Python 3 has made `bytes` and `str` (mostly) incompatible.

You can use `.decode([charset, error])` to convert `bytes` to `str`
and `.encode([charset, error])` to convert `str` to `bytes`.

Because `base64.encode` gives you a sequence of values between 0 and 127,
you can omit the optional parameters for `.decode`.


More information about the Python-list mailing list