Hi all,
I am having no end of issues with this so thought I would ask for some help guidance.
I want to convert a byte string that is send in over Serial i.e. '192' to its byte value 192. So a direct conversion as its a byte that's being sent not a string containing 192.
I have tried various methods and none seem to be working or I'm doing something wrong.
void setup () {
Serial.begin(9600);
}
void loop(){
// Do stuff
}
void serialEvent() {
char str[3];
byte c = 0x0;
int l = 0;
while (Serial.available() > 0) {
c = Serial.read();
str[l] = c;
l++;
}
for(int i =0;i<l;i+=2) {
byte sendb = getVal(str[i]) >> 4) + getVal(str[i + 1]);
Serial.write(sendb);
}
}
byte getVal(char c)
{
if(c >= '0' && c <= '9')
return (byte)(c - '0');
else if (c >= 'a' && c <= 'f')
return (byte)(c-'a'+10)
else
return (byte)(c-'A'+10)
}
Above is my latest iteration which doesn't work at all tbh.
Thanks for any replies.
Modified to include full code