Hi all!!
My memory fails and I forget the trick to convert these:
char mystring[] = "FF0FA101"
uint8_t myuint[5];
I need to extract pair by pair each string to convert to uint8_t with the strtoul()
Best Regards!!
Hi all!!
My memory fails and I forget the trick to convert these:
char mystring[] = "FF0FA101"
uint8_t myuint[5];
I need to extract pair by pair each string to convert to uint8_t with the strtoul()
Best Regards!!
void setup() {
Serial.begin(115200);
Serial.println("Beginning");
char mystring[] = "FF0FA101";
uint8_t myuint[4];
for (uint8_t i=0; i<strlen(mystring)/2;i++) {
myuint[i] = 0;
for (uint8_t j=0; j<2; j++) {
char firstchar = mystring[(i*2)+j];
//printf("myuint[%d] = %3d mystring[%d+%d] = %c ", i, myuint[i], i, j, mystring[(i*2)+j]);
if (firstchar >= '0' && firstchar <= '9') {
//Serial.print("Number");
myuint[i] = myuint[i]*16 + firstchar - '0';
} else if (firstchar >= 'A' && firstchar <= 'F') {
//Serial.print("LETTER");
myuint[i] = myuint[i]*16 + firstchar - 'A' + 10;
} else if (firstchar >= 'a' && firstchar <= 'f') {
//Serial.print("letter");
myuint[i] = myuint[i]*16 + firstchar - 'a' + 10;
} else {
// error
Serial.println("NOOOO");
}
//printf(" myuint[%d] = %3d\n", i, myuint[i]);
}
}
for (uint8_t i=0; i<strlen(mystring)/2; i++) {
Serial.println(myuint[i], HEX);
}
Serial.println("Ending");
}
void loop() {}
you could also probably use the sscanf or strtoul functions
That was the trick.... the i=+2!
I code this :
void loop(){
int i;
char tmp[3];
char buf[] = "4142434445";
tmp[2] = '\0';
uint8_t tx_buffer[20];
uint8_t len_buffer=0;
for(i=0;i<strlen(buf);i+=2) {
tmp[0] = buf[i];
tmp[1] = buf[i+1];
tx_buffer[len_buffer] = strtol(tmp,NULL,16);
len_buffer++;}
Serial.println(len_buffer);
for (i=0; i<len_buffer; i++){
Serial.write(tx_buffer[i]);
Serial.print(" ");
}
delay(1000);
Serial.println();
}
The value:4142434445, this is ABCDCE leters in hex string format
What you think about my coding?
Best Regards!!
Frank
FrankRadio:
That was the trick.... the i=+2!I code this :
void loop(){
int i;
char tmp[3];
char buf[] = "4142434445";
tmp[2] = '\0';
uint8_t tx_buffer[20];
uint8_t len_buffer=0;
for(i=0;i<strlen(buf);i+=2) {
tmp[0] = buf[i];
tmp[1] = buf[i+1];
tx_buffer[len_buffer] = strtol(tmp,NULL,16);
len_buffer++;}
Serial.println(len_buffer);
for (i=0; i<len_buffer; i++){
Serial.write(tx_buffer[i]);
Serial.print(" ");
}
delay(1000);
Serial.println();
}
The value:4142434445, this is ABCDCE leters in hex string format What you think about my coding?
Looks like it works, but you should work on lining everything up -- make everything in the same block (section of code with {}) line up, and then intent after a { and put each } on a new line. Or follow some other coding standard, I don't care. Just make it line up and indent.
Also, you could make it ever so slightly more efficient by making tmp just a char (not an array) and then using it to store the third character, setting that character to NULL, and then restoring it after strtol.
char buf[] = "4142434445";
uint8_t tx_buffer[20];
uint8_t len_buffer=0;
for(i=0;i<strlen(buf);i+=2) {
char tmp = buf[i+3];
buf[i+3] = 0;
tx_buffer[len_buffer] = strtol(buf,NULL,16);
len_buffer++;
buf[i+3] = tmp;
}