Skip to main content
Aabc
Associate III
November 11, 2021
Question

how to convert received uint8_t array to string in stm32 ?

  • November 11, 2021
  • 5 replies
  • 8433 views

I am receiving data from UART in uint8_t array.

I want to convert array to string to work on it:

if (Received Data=="Reset") {  resetFunc();  }

if (Received Data.substring(0,8)=="ABCDASD")

{

Do something

 }

if (Received Data.substring(4,8)=="ABC1234")

{

 int Number=1234;         // convert substring to int

char Data ="ABC";         // convert substring to char

}

This topic has been closed for replies.

5 replies

KnarfB
Super User
November 11, 2021

You cannot compare strings in C.

You can cast (uint8_t*) to (char*) ans use strcmp thereafter.

hth

KnarfB

TDK
November 11, 2021
if (strcmp((const char *) Received Data, "Reset") == 0) {
 resetFunc();
}
if (strncmp((const char *) Received Data, "ABCDASD", 7) == 0) {
 Do something
}
if (strncmp((const char *) &Received Data[4], "ABC1234", 4) == 0) {
 ...
}

"If you feel a post has answered your question, please click ""Accept as Solution""."
Aabc
AabcAuthor
Associate III
November 11, 2021

It worked

Very thanks

Tesla DeLorean
Guru
November 11, 2021

This is more a C language issue, and not STM32 specific

You can construct/re-construct a string a character at a time.

You might need to synchronize and discard characters if you receive from a serial port.

To use string functions you need to insure the data is NUL terminated.

Review C libraries and documentation. Review materials on parsing input.

For numeric conversion there are functions like atoi() and atod(), also sscanf()

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Aabc
AabcAuthor
Associate III
November 11, 2021

Thanks for all.