I would like to remove ASCII characters from my bytearray and see it containing hexadecimal numbers. To explain it better: I receive CAN messages from CANbus using the pyton-can library and catch them to store in a message variable. As I print it I see:
bytearray(b'\x00\x00^]b\tb\xd9')
But I don't want to see ASCII characters, I want it to print this as for example b'\x00\x00\x01\x65\xA5\x12\x1A\xBB
So what should I do?
My code now looks like:
#receiving CAN Bus messages here
bus = can.interface.Bus(interface='kvaser', channel=0, bitrate=1000000)
byte_index_to_check = 1
msg = can.Message(arbitration_id=0x007)
bus.send(msg)
recvMsg = bus.recv(timeout=0.5)
message = bus.recv()
received_message = hex(message.data[byte_index_to_check])
last_message = message
last_received = received_message
print("last message",last_message.data)
You don't so much want to remove characters, as convert them to HEX. The
binasciilibrary does just that: