I need to convert a simple string to a byte array which uses hex representation, just like that site:
http://string-functions.com/string-hex.aspx
This string then gets send via bluetooth using python and the arduino reads the individual bytes like this:
char buff[1000];
int i =0;
int typeByte = serial->read();
int data = serial->read();
while (true) {
if (data == -1) continue;
if (data == 254 || data == 10) break;
buff[i++] = data;
data = serial->read();
delay(10);
}
String buffer(buff);
if(buffer.startsWith("AUTH")){
dostuff();
}
Those are then stored in a char[] array and used for comparison with a command name.
This is the code I am using in the Python Project (message is a string for example "AUTH")
self.bluetooth_socket.send(b"\x01" + message.encode('ascii') + b"\x10")
This is what the arduino receives:
01 65 85 84 72 16
But it should look like that:
01 41 55 54 48 10
I know that the second byte array is basically the first one just in hex representation - how would I achieve that?
解决方案
Yeah, in the first array you sent the ascii values.
To get the hex values in python3:
>>> import codecs
>>> codecs.encode(message.encode("ascii"), "hex")
b'41555448'