Here is an example code:
double A = 0;
QString str;
str.sprintf("%.3f",A)
How to rewrite the code str.sprintf("%.3f",A) using the arg function of QString?
Argument a in QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' ')) const is formatted according to the specified format and precision. See Argument Formats for details.
I your example:
double A = 0;
QString str("%1");
str = str.arg(A,0,'f',3);
Related
I have a few question, how do I convert Float to String?
Because my OLED display require 'String' and cannot print Float
Here is my coding
WindSpeed = WIND_SPEED_20_PULSE_SECOND / ONE_ROTATION_SENSOR * (float) Rotations;
float SpeedMPH = ((WindSpeed * 3600) / CONVERTMPH_FORMULA);
String WindSpeedMS = WindSpeed;
if((millis() - Start_Read_Timer) >= READ_TIME)
{
cli();
WindSpeedStatus();
sei();
Rotations = 0;
Start_Read_Timer = millis();
}
display.setFont(ArialMT_Plain_24);
display.drawString(0, 20, WindSpeedMS);
display.display();
delay(500);
The error I got is:
Compilation error: conversion from 'float' to non-scalar type 'String'
requested
Thanks!
Try
float SpeedMPH = ((WindSpeed * 3600) / CONVERTMPH_FORMULA);
String WindSpeedMS = String(SpeedMPH,0); // 2nd param is decimal digits
refer to Arduino String library as they stated that :
Syntax
String(val)
String(val, base)
String(val, decimalPlaces)
Parameters
val: a variable to format as a String. Allowed data types: string,
char, byte, int, long, unsigned int, unsigned long, float, double.
base: (optional) the base in which to format an integral value.
decimalPlaces: only if val is float or double. The desired decimal
places.
so instead of writing :
String WindSpeedMS = WindSpeed;
you should write :
String WindSpeedMS = String(WindSpeed, 5);
where 5 represents the number of decimal places desired, so if WindSpeed = 12.54545451 then WindSpeedMS = "12.54545"
The usual hash-functions, e.g. from digest create hex output. I want to create a hash with character from a given set, e.g [a-z,0-9]; no strong cryptographic security is required.
Using base64encode on a hashed string comes close, but the character set is fixed in that function.
It is ugly div/mod manipulation for an arbitrary character table, so I decided to use a 32 character table without l 0, O
#include <Rcpp.h>
using namespace Rcpp;
static const std::string base32_chars = "abcdefghijkmnpqrstuvwxyz23456789";
// [[Rcpp::export]]
String encode32(uint32_t hash_int, int length = 7)
{
String res;
std::ostringstream oss;
if (length > 7 || length < 1)
length = 7;
for (int i = 0; i < length; i++) {
oss << base32_chars[hash_int & 31];
hash_int = hash_int >> 5;
}
res = oss.str();
return res;
}
/*** R
print(encode32(digest::digest2int("Hellod")))
*/
There is an issue using QString::arg() when a string contains a digit right after a place marker. It's not clear from the QString::arg() function description what would happen in case of such a replacement:
QString("String for replacement %1234").arg("blah");
Will this result in "String for replacement blah234" or "String for replacement blah34"?
I looked in the QT's source code to answer this question. It seems that the algorithm which looks for place markers is 'greedy' and it would take both digits in the example above.
Here is the source of the QT's function which is used inside the QString::arg() (QT 4.8.4):
static ArgEscapeData findArgEscapes(const QString &s)
{
const QChar *uc_begin = s.unicode();
const QChar *uc_end = uc_begin + s.length();
ArgEscapeData d;
d.min_escape = INT_MAX;
d.occurrences = 0;
d.escape_len = 0;
d.locale_occurrences = 0;
const QChar *c = uc_begin;
while (c != uc_end) {
while (c != uc_end && c->unicode() != '%')
++c;
if (c == uc_end)
break;
const QChar *escape_start = c;
if (++c == uc_end)
break;
bool locale_arg = false;
if (c->unicode() == 'L') {
locale_arg = true;
if (++c == uc_end)
break;
}
if (c->digitValue() == -1)
continue;
int escape = c->digitValue();
++c;
if (c != uc_end && c->digitValue() != -1) {
escape = (10 * escape) + c->digitValue();
++c;
}
if (escape > d.min_escape)
continue;
if (escape < d.min_escape) {
d.min_escape = escape;
d.occurrences = 0;
d.escape_len = 0;
d.locale_occurrences = 0;
}
++d.occurrences;
if (locale_arg)
++d.locale_occurrences;
d.escape_len += c - escape_start;
}
return d;
}
Is there a better way of solving such an ambiguity than always using a 2-digit place markers?
Since you can only use %1 to %99 as markers and you can skip marker numbers you can write:
QString("String for replacement %10234").arg("blah");
to output String for replacement blah234
Qt help states for arg(const QString & a, int fieldWidth = 0, QChar fillChar = QLatin1Char( ' ' ))
Returns a copy of this string with the lowest numbered place marker replaced by string a, i.e., %1, %2, ..., %99.
...
Place marker numbers must be in the range 1 to 99.
Therefore, what you're seeing is, by definition, correct; the first two numbers will be replaced. If you're wanting "String for replacement blah234", then you could define the string as: -
QString("String for replacement %1%2").arg("blah").arg(234);
I have the same issue, but the order answers not looks like a good way for me.
I have resolve the ambiguity in this way.
QString myString= QString("ConcatenatedNumbers%0123").arg(66,3,10, QChar('0'));
The string will be:
ConcatenatedNumbers06623
I have this code
if(genericPtr->type == XML_ATTRIBUTE_NODE){
xmlAttrPtr attr = (xmlAttrPtr)genericPtr;
printf("\n str %s\n", (const char *)attr->children->content);
}
xmlBufferPtr bufferPtr = xmlBufferCreate();
if (IsXmlNsPtr(genericPtr)){
xmlNodeDump(bufferPtr, NULL, (xmlNodePtr)genericPtr, 0, format);
}else{
xmlNodeDump(bufferPtr, ((xmlStdPtr)genericPtr)->doc, (xmlNodePtr)genericPtr, 0, format);
}
printf("\n str2 %s\n", (const char *)bufferPtr->content);
The result is
str чатрум#muc.chat.quickblox.com/300
str2 to="чатрум#muc.chat.quickblox.com/300"
In result i need чатрум instead
чатрум
how can i do this?
Try xmlNodeDumpOutput which allows you to specify an encoding. Note that it uses an xmlOutputBuffer instead of an xmlBuffer. Another useful function is xmlXPathCastNodeToString which returns the XPath string value of a node as xmlChar.
How I can count characters in QByteArray, for example I have QByteArray and I want to know how many "*" in this array.
From QByteArray documentation:
int QByteArray::count ( const char * str ) const
This is an overloaded function.
Returns the number of (potentially overlapping) occurrences of string str in the byte array.
count.
You could use QByteArray::indexOf(char ch, int from = 0) const inside a loop.
Maybe this:
int i = 0, counter = 0;
while((i = array.indexOf("*", i)) >= 0)
counter++;