Convert string to int in xcode - xcode4.5

I have two text fields.I need to convert text values into int. So i can add two values and display in result text field.Somebody please help me to convert string to integer.

You don't specify whether you're using iOS or OSX, which doesn't help answer the question.
Regardless, if you're using OSX, then NSTextField derives from NSControl which provides an intValue method:
int number = [myTextField intValue];
(reference).
And if you're using iOS then UITextField provides its value via the text property (NSString) which provides an intValue method:
int number = [[myTextField text] intValue];
(reference).
There are variations of these methods which also provide their value as float, double, etc.

int *vla = (int)[string number here];

Related

How to read numbers; How to store those numbers in variable; and how to display those numbers in a label; from line edit in Qt?

I have to read numbers from line edit in Qt Creator, and then divide them by 100 and display them in a label.
The only method I know is:
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
ui->label_OOP_marks->setText(QString(OOP_marks));
But the above method cannot read numbers; it reads string. I have tried a lot but can't figure out the code for this part of the program.
You can use QString::toDouble for converting string to number. If you are planning to accept integers use QString::toInt instead. To convert number to string again QString::number is way to go.
const auto& OOP_marks = ui->lineEdit_OOP_marks_input->text();
bool isNumber; // optional
const double number = OOP_marks.toDouble(&isNumber);
const auto& result = isNumber ? QString::number(number / 100) : QString("Please enter valid number.");
ui->label_OOP_marks->setText(result);
But I suggest you to use QSpinBox or QDoubleSpinBox, instead of QLineEdit.
How about QString toInt function, and then static QString::number function?
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
int OOP_marks_val = OOP_marks.toInt();
ui->label_OOP_marks->setText(QString::number(OOP_marks_val/100));

Im having trouble concatenating a string with an int

I have the below code to read a light sensor, convert to lux, concat with "lux." and send it to my SmartThings cloud.
Ultimately I want a value sent to SmartThings formatted like lux.110
void checkLux() {
float logLux = analogRead(lightPIN) * logRange / rawRange;
int luxValue = pow(10, logLux);
String statusUpdate = "lux." + luxValue;
Serial.println(statusUpdate);
smartthing.send(statusUpdate);
delay(1000);
}
This above code spits out some weird combination of characters to the serial monitor and doesnt print lux. or the luxvalue.
If I add this line String luxString = "lux."; and modify the line below, it all works great. Any thoughts on why I need to declare this string separately. According to the documentation either should work fine.
Also if there are any suggestions on variable savings within this block of code. I am not that great at it yet.
As Arduino just uses C++ most C++ functions will also work so dont just limit yourself to Arduino's reference pages.
Apparently the String constructor doesn't support numbers, you must convert them using the String() function first as seen here.
Alternatively I think you can append a string like this:
String statusUpdate = "lux.";
statusUpdate += luxValue;
as seen here,
which is the same as using String's concat function.
statusUpdate.concat(luxValue);

Qt Check QString to see if it is a valid hex value

I'm working with Qt on an existing project. I'm trying to send a string over a serial cable to a thermostat to send it a command. I need to make sure the string only contains 0-9, a-f, and is no more or less than 6 characters long. I was trying to use QString.contains, but I'm am currently stuck. Any help would be appreciated.
You have two options:
Use QRegExp
Use the QRegExp class to create a regular expression that finds what you're looking for. In your case, something like the following might do the trick:
QRegExp hexMatcher("^[0-9A-F]{6}$", Qt::CaseInsensitive);
if (hexMatcher.exactMatch(someString))
{
// Found hex string of length 6.
}
Update
Qt 5 users should consider using QRegularExpression instead of QRegExp:
QRegularExpression hexMatcher("^[0-9A-F]{6}$",
QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = hexMatcher.match(someString);
if (match.hasMatch())
{
// Found hex string of length 6.
}
Use QString Only
Check the length of the string and then check to see that you can convert it to an integer successfully (using a base 16 conversion):
bool conversionOk = false;
int value = myString.toInt(&conversionOk, 16);
if (conversionOk && myString.length() == 6)
{
// Found hex string of length 6.
}

Qt ComboBox->addItem() Integer to Qstring conversion error?

I am really new to Qt and I have a little question for you. I am trying to work on ComboBox and when I add items to a combobox an integer like;
combobox->addItem(class.value); // class.value is an integer
It just adds a symbol to the combobox (*, / or ? )
How can I solve this little problem ?
Try combobox->addItem(QString::number(class.value));
Use QVariant . Advantage of using QVariant over QString::number() is you can convert data of any type to any other type.
int to string
QVariant(32).toString(); //assuming calss.value to be int
in your case it will be
combobox->addItem(QVariant(class.value).toString());
float to a string
QVariant(3.2).toString();
string to a float:
QVariant("5.2").toFloat();
it is that easy.

How to convert TBuf8 to QString

I've tried to convert using the following code:
template< unsigned int size >
static QString
TBuf82QString( const TBuf8< size > &buf )
{
return QString::fromUtf16(
reinterpret_cast<unsigned short*>(
const_cast<TUint8*>(
buf.Ptr() ) ), buf.Length() );
}
But It always returns something like ?????b.
EDIT: Changed code example
Using a template probably isn't a good solution, since it will result in a new instantiation of this block of code within your application binary, for every size of input string which is converted. Since the output type (QString) contains no compile-time constant, this means you end up with code bloat, for no gain.
A better approach would be to leverage the fact that TBuf8<N> inherits from TDesC8:
QString TBuf2QString(const TDesC8 &buf)
{
return QString::fromLocal8Bit(reinterpret_cast<const char *>(buf.Ptr()),
buf.Length());
}
TBuf<16> foo(_L("sometext"));
QString bar = TBuf2QString(foo);
TBuf8 is used for binary data or non-Unicode strings. TBuf16 is used for Unicode strings. TBuf is conditionally compiled and will always be TBuf16 as Symbian OS is natively Unicode.
Try using QString::fromLocal8Bit() with TBuf8::Ptr()

Resources