Convert String to uint8_t for Arduino C++ - arduino

I'm trying to send a user input String type over Bluetooth with function
BluetoothSerial SerialBT;
SerialBT.write()
But the function won't accept String, but instead it accepts uint8_t.
I tried some conversions like
char buf[packet.length()];
memcpy(buf, packet.c_str(), packet.length());
SerialBT.write(buf, packet.length());
But it shows an error
invalid conversion from 'char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]
Could please someone show me how to convert it right?
Thanks

Related

QString.toInt() doesnt work - Error: invalid operands to binary expression ('const char *' and 'const char [14]')

I have this code:
QString carda = "000123";
QString queryStringAnet("SELECT * FROM [records] WHERE ([user]='" + carda.toInt() + "' AND [apl]='"+apl+"' AND [tasktype]='"+taskType+"' AND [taskkind]='"+taskKind+"' AND [timestamp]='"+timestamp+"')");
and for the conversion from QString to Int when I use carda.toInt() Im having this error:
error: invalid operands to binary expression ('const char *' and
'const char [14]')
and warnings:
warning: adding 'int' to a string does not append to the string
use array indexing to silence this warning
I dont understand why QString.toInt() wont be working... any idea?
I dont understand why QString.toInt() wont be working... any idea?
the problem is that in qt you just can't concatenate together strings and numbers...
and actually you dont even need to convert the string carda to integer because that is a QString
instead just do:
QString queryStringAnet("SELECT * FROM [records] WHERE ([user]='" + carda + "' AND [apl]='"+apl+"' AND [tasktype]='"+taskType+"' AND [taskkind]='"+taskKind+"' AND [timestamp]='"+timestamp+"')");

Convert Hex String Color to uint_16

I need to convert a HEX string to uint_16 in order to use a fillColor method for the m5Stack hardware.
I'm currently fetching the HEX color value via a GET request to https://m5stack.glitch.me/getColor
I tried
uint16_t color = (uint16_t) strtol(http.getString(), NULL, 16);
But am getting the error
cannot convert 'String' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'
How can I take a string HEX color value and convert it to uint_16?
The strtol() can't handle a String Object as input. You must convert it into a character array.
strtol(http.getString().c_str(), NULL, 16);

arduino, error: invalid operands of types 'char [14]' and 'char [5]' to binary 'operator+'

I have a problem with some code.
Have read a tons of topics, but most are them are related to custom libraries.
My code is not related to any custom libraries.
I hope some of you know what im doing wrong.
I'm simply trying to "merge" two strings into a new variable.
Error:
sketch_SS01:13: error: invalid operands of types 'char [14]' and 'char [5]' to binary 'operator+'
char apiPath = apiPage + pid;
^
exit status 1
invalid operands of types 'char [14]' and 'char [5]' to binary 'operator+'
Error related to this code:
// api details
char apiPage[] = "/api.php?pid=";
char pid[] = "8855";
char apiPath = apiPage + pid;
The compiler says it: you cannot use operator+ to concatenate C strings (i.e. char[]). You need to use the library function strcat or it's safer sibling strncat.
The concatenation of a string x onto the string dest is strcat (dest,x); but please consult the documentation and pay extra attention to the risk of buffer overflows when dealing with char arrays.
To write your example as it is written you can do
// api details
char apiPage[] = "/api.php?pid=";
char pid[] = "8855";
char apiPath[100] = ""; // make sure it' long enough and initialized to empty string
strcat(apiPath, apiPage);
strcat(apiPath, pid);
or you could copy the strings using to the correct place in the destination string usingstrcpy or strncpy.
Addition:
A (perhaps better/simpler/safer) alternative is to use the String class which has all the expected string functionality (like constructors, add, append, etc.): see https://www.arduino.cc/en/Reference/StringObject

Arduino: Write.Server -> int to byte array

I am trying to send an integer from my Arduino Mega to my Android app. I am trying to split the int into two bytes which my Android app will then recieve in a buffer with a size of 16384 (two bytes). I've just started with arduino so I'm a bit lost!
So far I have this:
int val = analogRead(A0); // as states int value from 0 - 1023
byte high = highByte(val);
byte low = lowByte(val);
byte byteArray[2] = {high, low};
server.write(16384, byteArray);
The error I get is:
ProArd.ino: In function 'void loop()':
ProArd:88: error: invalid conversion from 'int' to 'const uint8_t*'
ProArd:88: error: initializing argument 1 of 'virtual size_t WiFiServer::write(const uint8_t*, size_t)'
ProArd:88: error: invalid conversion from 'byte*' to 'size_t'
ProArd:88: error: initializing argument 2 of 'virtual size_t WiFiServer::write(const uint8_t*, size_t)'
You are passing in the array itself as a pointer into the size parameter of the write function, which only takes a positive int.
Try using this to get the length of the array: sizeof(arr);

Invalid conversion from char to 'uint8_t'

I'm using a shift out statement to drive a few 7 seg displays (in the end)
but I'm running into a problem.
I have used #include <avr/pgmspace.h> libary as to save space for the processing.
At the end of it I need to shiftOut a binary number to be fed into a reg then to a BCD then to my display:
strcpy_P(buffer, (char*)pgm_read_word(&(Times[big])));
shiftOut(dataPin, clockPin, MSBFIRST, buffer);
in the buffer place will go the selected value (e.g. B00100011 should display 23),
my code gives me
Invalid conversion from char to 'uint8_t'
with the shiftOut line highlighted
Any ideas?
The problem here is that shiftOut expects a byte (uint8_t) as its 4th argument (value). The passed value is a char* (presumably declared as a prog_char array).
To fix this, the declaration of the value will need to use prog_uchar, like this:
prog_uchar values[] PROGMEM = { (prog_uchar) B00100011, ... };
...
int valueIndex = ...; // Index of value in the values array
shiftOut(dataPin, clockPin, MSBFIRST, pgm_read_byte(&(values[valueIndex])));

Resources