I'm trying to concatenate string and bytes in Python. This isn't working, but I think it could work if we could get rid of the b''
and treat the bytes object as a string:
import binascii
s = 'hello world'
hex_code = binascii.hexlify(bytes(s, 'utf-8'))
print(hex_code) # b'68656c6c6f20776f726c64'
# this would work
print('68656c6c6f20776f726c64' + ' some other string')
# this fails
print(hex_code + ' some other string')
My ultimate use case is for something totally unrelated. SQL Server supports setting metadata by issuing a `SET CONTEXT_INFO' command which requires the metadata be encoded. Ideally I want to construct this metadata from my application. My command might look something like:
import bin2ascii
def set_context(conn, context_info: str):
hex_string = binascii.hexlify(bytes(context_info, 'utf-8'))
conn.execute('SET CONTEXT_INFO 0x' + hex_string)
This of course isn't working because I can't figure a way to concatenate string and bytes.