(3)
++上面的函数太大,下面给出举例:
++
++
++
++
++
++
++给出上图中的函数的源码依据 :
// note - this are all inline so we can benefit from
//strlen() compile time optimizations 以下全是静态的成员函数
static QString fromLatin1( QByteArrayView ba);
Q_WEAK_OVERLOAD
static inline QString fromLatin1(const QByteArray & ba)
{ return fromLatin1(QByteArrayView(ba)); }
//Returns a QString initialized with the first size characters of the
//Latin-1 string str.
//If size is -1, strlen(str) is used instead.
static inline QString fromLatin1(const char * str, qsizetype size)
{
return fromLatin1(QByteArrayView(str, !str || size < 0
? qstrlen(str) : size));
}
static QString fromUtf8( QByteArrayView utf8);
Q_WEAK_OVERLOAD
static inline QString fromUtf8(const QByteArray & ba )
{ return fromUtf8(QByteArrayView(ba)); }
static inline QString fromUtf8(const char * utf8, qsizetype size)
{
return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0
? qstrlen(utf8) : size));
}
static QString fromLocal8Bit( QByteArrayView ba);
Q_WEAK_OVERLOAD
static inline QString fromLocal8Bit(const QByteArray & ba)
{ return fromLocal8Bit(QByteArrayView(ba)); }
static inline QString fromLocal8Bit(const char * str, qsizetype size)
{
return fromLocal8Bit(QByteArrayView(str, !str || size < 0
? qstrlen(str) : size));
}
(4)介绍另一个重要的静态函数, number():
//Sets the string to the printed value of n in the specified base,
//and returns a reference to the string.
//The base is 10 by default and must be between 2 and 36.
//The formatting always uses QLocale::C, i.e., English/UnitedStates.
//To get a localized string representation of a number,
//use QLocale::toString() with the appropriate locale.
QString & setNum(short n, int base=10)
{ return setNum(qlonglong(n), base); }
QString & setNum(ushort n, int base=10)
{ return setNum(qulonglong(n), base); }
QString & setNum(int n, int base=10)
{ return setNum(qlonglong(n), base); }
QString & setNum(uint n, int base=10)
{ return setNum(qulonglong(n), base); }
QString & setNum(long n, int base=10)
{ return setNum(qlonglong(n), base); }
QString & setNum(ulong n, int base=10)
{ return setNum(qulonglong(n), base); }
QString & setNum(qlonglong , int base=10);
QString & setNum(qulonglong, int base=10);
QString & setNum(float n, char format='g', int precision=6)
{ return setNum(double(n),format,precision); }
QString & setNum(double, char format='g', int precision=6);
//Sets the string to the printed value of n,
//formatted according to the given format and precision,
//and returns a reference to the string.
//Returns a string equivalent of the number n according to the specified base.
//The base is 10 by default and must be between 2 and 36.
//For bases other than 10, n is treated as an unsigned integer.
//The formatting always uses QLocale::C, i.e., English/UnitedStates.
//To get a localized string representation of a number,
//use QLocale::toString() with the appropriate locale.
static QString number(int, int base=10);
static QString number(uint, int base=10);
static QString number(long, int base=10);
static QString number(ulong, int base=10);
static QString number(qlonglong, int base=10);
static QString number(qulonglong, int base=10);
static QString number(double n, char format='g', int precision=6);
//Returns a string representing the floating-point number.
//Returns a string that represents n,
//formatted according to the specified format and precision.
//For formats with an exponent,
//the exponent will show its sign and have at least two digits,
//left-padding the exponent with zero if needed.
++实验举例:
(5)接着介绍字符串到数值的转换函数:
//Returns the string converted to an int using base base,
//which is 10 by default and must be between 2 and 36, or 0.
//Returns 0 if the conversion fails.
//If ok is not nullptr, failure is reported by setting *ok to false,
//and success by setting *ok to true.
//If base is 0, the C language convention is used:
//If the string begins with "0x", base 16 is used;
//if the string begins with "0", base 8 is used; otherwise, base 10 is used.
//The string conversion will always happen in the 'C' locale.
//For locale-dependent conversion use QLocale::toInt()
short toShort (bool * ok=nullptr, int base = 10) const
{ return toIntegral_helper<short>(*this, ok, base); }
ushort toUShort (bool * ok=nullptr, int base = 10) const
{ return toIntegral_helper<ushort>(*this, ok, base); }
int toInt (bool * ok=nullptr, int base = 10) const
{ return toIntegral_helper<int>(*this, ok, base); }
uint toUInt (bool * ok=nullptr, int base = 10) const
{ return toIntegral_helper<uint>(*this, ok, base); }
long toLong (bool * ok=nullptr, int base = 10) const
{ return toIntegral_helper<long>(*this, ok, base); }
ulong toULong (bool * ok=nullptr, int base = 10) const
{ return toIntegral_helper<ulong>(*this, ok, base); }
qlonglong toLongLong (bool * ok=nullptr, int base = 10) const;
qulonglong toULongLong(bool * ok=nullptr, int base = 10) const;
float toFloat (bool * ok=nullptr ) const;
double toDouble (bool * ok=nullptr ) const;
++ 举例:
(6)
谢谢