Convert Hex String Color to uint_16 - arduino

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);

Related

Convert String to uint8_t for Arduino C++

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

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+"')");

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

Implicit conversion of a non-Objective-C pointer type 'char *' to 'NSString *' is disallowed with ARC

For the following line of code I am getting the error below:
for (UILabel *label in labels) {
label.text = label.tag - 100 > someMutableString.length ? "" : "*";
}
The error states:
Implicit conversion of a non-Objective-C pointer type 'char *' to 'NSString *' is disallowed with ARC
My variable "someMutableString" is of type NSMutableString.
How do I fix in my particular case?
The problem is that your string literals are "" and "*" which are both C-style strings (const char*). So the type of the right hand side of the assignment is also const char*. You are assigning to the text property of a UILabel, which takes an NSString.
Use #"" and #"*" instead.
char *text = "a"
NSString *message = [NSString stringWithFormat:#"%s",text];
Cheers :)

Qt - Add two numbers

I can't get this to work. Anyone knows how to make it work?
void MainWindow::on_pushButton_clicked()
{
int sum1 = ui->lineEdit->text().toInt();
int sum2 = ui->lineEdit_2->text().toInt();
ui->label_4->setText(sum1 + sum2);
}
Error:
C:\Qt\Tools\QtCreator\bin\Mellemrubrik\mainwindow.cpp:26: error: C2664: 'QLabel::setText' : cannot convert parameter 1 from 'int' to 'const QString &'
Reason: cannot convert from 'int' to 'const QString'
No constructor could take the source type, or constructor overload resolution was ambiguous
In general, you can convert multiple numeric types to QStrings like so:
int val1, val2;
QString result = QString("val1=%1 val2=%2 sum=%3").arg(val1).arg(val2).arg(val1+val2);
But for numbers, this is also possible:
int val1, val2;
QString result = QString::number(val1+val2);
You can see Qt's documentation for more info!
You can try this
int v1,v2;
v1=ui->lineEdit->text().toInt():
v2=ui->lineEdit_2->text().toInt()
QString result = QString::number(v1+v2);
ui->label->setText(result);
Would setNum as shown below do?
int v1,v2;
v1=ui->lineEdit->text().toInt():
v2=ui->lineEdit_2->text().toInt()
ui->label->setNum(v1+v2);

Resources