I have this function in my program
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
if (event == ADB_CONNECTION_RECEIVE)
for (i=0; i<length; i++)
and I want to convert the uint8_t *data into a string or char array to be able to compare it with a specific string. How can I convert it?
I already tried something like
but what I obtain is strange symbols at the line “Serial.print(input_cr);”. What am I doing wrong?
ps: sorry I am just novel in programming…
Don't covert it, just cast it:
uint8_t data = "Hello!";
int val = strcmp((char *) data, "Hello!");
Ditch the reference symbol.
Arrch already gave you the answer, but just to reinforce his answer, try to think of something in memory as a bucket. For example, suppose you have the following definitions in your code:
int myNumber = 10;
And imagine that the compiler puts myNumber at memory address 500, which is also called its lvalue (i.e., location value). If we go to memory address 500 and look inside, we see the value of 10, also called its rvalue (i.e., is register value). The data type specifier (int) in the statement tells you the data item's size; it's a 2-byte bucket. Now consider:
ptr = &myNumber;
val = *ptr;
The address of operator (&) in front of myNumber tells you that the bucket is located at memory address 500, so the statement places 500 in the variable ptr as its rvalue. After all, a valid pointer can only hold a memory address or null, not a normal data value. The indirection operator (*) says: "Goto to the memory address stored in ptr's rvalue (500), and fetch what you find there." Since ptr is an int pointer, the compiler knows to grab two bytes from address 500 and move those bytes into the rvalue of val.
In your code, by having the address of operator in from of data, you were telling the compiler to treat the byte stored at data as a memory address, rather than a pointer to some data. That's why the compiler was complaining.
Thanks both That fixed my problem.
I actually have a question though. The value passed in the function is uint8_t * data, in which data is a memory location. So why Serial.print((char) data*) prints the content of that memory location?*