QString with special characters to const char* - qt

I m trying to convert QString with special characters to const char* but I did not succeed. my function is:
void class::func(const QString& Name) // fileName = "â.tmp"
{
qDebug()<< Name; // display "â.tmp"
const char* cfileName = Name.toAscii().data();
qDebug() << cfileName; // display "a?.tmp"
}
qDebug()<< fileName display the true value that is "â.tmp" but after converting it to const a char*, I do not succeed to have the right value.
In the second time I try to use const char* cfileName = QString::fromUtf8(fileName.toAscii().data()); but I did not still have the right value, it display the same thing: "a?.tmp". How can I fix this thank you

due to convert QString to const char* :
QString str("hi lor!");
const char *s = str.toStdString().c_str();
msg.setText(QString::fromUtf8(s));
msg.exec();

EDIT: using QByteArray QString::toUtf8 () const is much better
QString string = "â.tmp";
const char* encodedString = string.toUtf8().data();
ORIGIONAL:
You probably need to use a codec, see http://qt-project.org/doc/qt-4.8/qtextcodec.html
something like this should work:
QString string = "â.tmp";
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QByteArray encodedString = codec->fromUnicode(string);
the documentation does not say what encoding types QDebug & QDebug::operator<< ( const char * s ) supports, it may be platform dependent, try verifying a correct conversion another. The problem may be in qDebug() or the stream it writes to.

Related

How can I get convert wstringstream to char*?

how convert wstringstream to char* ?? (Language c++)
I need this conversion to use the function writeRawData of the qdatastream.h library.
Thank you very much!!
You have to use wstringstream::str() to retrieve the content of the stream.
And then depending on your need you can either convert it to a QString so that the QDataStream can handle the string for you or just write the bytes of the wstring:
void f(wstringstream &stream, QDataStream &qstream)
{
wstring content = stream.str();
QString str = QString::fromStdWString(content);
qstream << str;
}
void g(wstringstream &stream, QDataStream &qstream)
{
wstring content = stream.str();
qstream.writeRawData(static_cast<const char *>(content.c_str()), content.length() * sizeof(wchar_t));
}
wstringstream is a basic_stringstream, you could extract line, char, text from it
look at that http://www.cplusplus.com/reference/sstream/wstringstream/
and you could retreive one example from that http://www.cplusplus.com/reference/sstream/basic_stringstream/str/

Convert text in other encoding qt

Alert: I am using QByteArray.
I want to ask about which other conversions exist. I am normally using toLatin1 but i would try with others. Example:
datoss = "|#|" + ui->textocuenta->text().toLatin1() + "|#|";
I repeat again, i am trying to use other conversion. Only that.
See QString class info. Convert your QByteArray to QString, there are multiple conversions:
CFStringRef toCFString() const
QString toCaseFolded() const
QString toHtmlEscaped() const
QByteArray toLatin1() const
QByteArray toLocal8Bit() const
NSString * toNSString() const
std::string toStdString() const
std::u16string toStdU16String() const
std::u32string toStdU32String() const
std::wstring toStdWString() const
ushort toUShort(bool *ok = Q_NULLPTR, int base = 10) const
QVector<uint> toUcs4() const
QByteArray toUtf8() const
int toWCharArray(wchar_t *array) const
If you are looking for a way to convert your indexed data from your QWidget in to a QByteArray, just use:
const QString indexed = QString("|#|%1|#|").arg(ui->textocuenta->text());
datoss = indexed.toLatin1();

Qt array QString

I get result from db by selectall query and I want save result in array and send it by socket.
db.open();
QSqlQuery *selectall = new QSqlQuery(db);
selectall->prepare("select * from phone_table");
selectall->exec();
selectall->first();
QString result;
QByteArray arrayresult;
int index = 0;
while (selectall->next())
{
index += 1;
// qint16 id = selectall->value(0).toString();
QString name_ = selectall->value(1).toString();
QString surname = selectall->value(2).toString();
QString phone_number = selectall->value(3).toString();
result = "*"+ name_+"*"+surname+"*"+phone_number;
arrayresult[index] = result;
}
I get this error binary '=' : no operator found which takes a right-hand operand of type 'const char [16]'
You are trying to set a QByteRef to a QString.
I think you may want a QList and to arrayresult.append(result).
Or else if you want one QByteArray with the concat of all results use arrayresult+= result.
You may build the QString you want to initialize QByteArray. To then convert from QString to QByteArray, you can do
QByteArray array_ = string_.toLatin1();
if encoding is Latin1.
You may alternatively use append
QByteArray & QByteArray::append ( const QString & str )
This is an overloaded function.
Appends the string str to this byte array. The Unicode data is
converted into 8-bit characters using QString::toAscii().
If the QString contains non-ASCII Unicode characters, using this
function can lead to loss of information. You can disable this
function by defining QT_NO_CAST_TO_ASCII when you compile your
applications. You then need to call QString::toAscii() (or
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
explicitly if you want to convert the data to const char *.
append is doing the same as + operator.
You can do the following with the toLatin1() function of the QString.
// ...
QString result = QString( "*%1*%2*%3" ).arg( name_ )
.arg( surname )
.arg( phone_number );
QByteArray resultArray = result.toLatin1();
// Or ...
// QByteArray resultArray = result.toLocal8Bit();
// QByteArray resultArray = result.toUtf8();
And you shall use a QList< QByteArray > for containing the results, or you can just append the last result item to your final result object.

QByteArray convert to/from unsigned char *

QByteArray inArray = " ... ";
unsigned char *in = convert1(inArray);
unsigned char *out;
someFunction(in, out);
QByteArray outArray = convert2(out);
the question is how can I correctly make these conversions (convert1 and convert2).
I cannot change someFunction(unsigned char *, unsigned char *), but I have to work with QByteArray here.
Qt has really great docs, you should use them.
If someFunction doesn't modify or store pointer to in data you can use this:
QByteArray inArray = " ... ";
unsigned char *out;
someFunction((unsigned char*)(inArray.data()), out);
QByteArray outArray((char*)out);
Otherwise you have to make a deep copy of the char* returned by QByteArray::data() (see the docs for code snippet).
if someFunction takes a const char* args then just use ConstData() or data() in QByteArray class.
if you need a char*, you can then use strdup(). This method is doing this
char *strdup (const char *s) {
char *d = malloc (strlen (s) + 1); // Space for length plus nul
if (d == NULL) return NULL; // No memory
strcpy (d,s); // Copy the characters
return d; // Return the new string
}
more info here: strdup() - what does it do in C?

How to convert LPTSTR to QString

Hi can anyone help me to convert LPTSTR to QString
You will see in the docs that Qstring provides static function to convert from both ascii and Unicode strings:
QString fromAscii ( const char *
ascii, int len = -1 )
QString fromLatin1 ( const char *
chars, int len = -1 )
QString fromUtf8 ( const char * utf8,
int len = -1 )
QString fromLocal8Bit ( const char *
local8Bit, int len = -1 )
QString fromUcs2 ( const unsigned
short * str )
Check whether you are using ascii or unicode and pick your poison.
QString::fromWCharArray is what worked for me.
To convert QString to LPTSTR or LPCTSTR:
QString src;
LPTSTR dest=(LPTSTR)src.utf16();
to convert from LPTSTR or LPCTSTR to QString :
src=QString::fromUtf16(dest);
The solution expects that your LPTSTR array is null-terminated.
STRRET str; // it had been filled by a winapi function
// str.pOleStr is LPWSTR, i.e. wchar_t* field of the union STRRET
// LPWSTR is the same as LPTSTR (see further)
QString result{QString::fromWCharArray(str.pOleStr)};
As shown below, STRRET is a union, one of representations of which (named pOleStr) has type wchar_t*.
As far as I understand, when one gets this STRRET filled by a Winapi function (like e.g. IShellFolder::GetDisplayNameOf()), it's a normal null-terminated wide string. So it can be supplied to QString::fromWCharArray(const wchar_t *string). The function will preserve the input wchar_t-array as it is and will just create a new copy inside a new QString. So overall, I think the method in my answer is quite safe as far as you know that your const wchar_t * is ended with a \0-wide-character.
// shtypes.h
typedef struct _STRRET
{
UINT uType;
/* [switch_is][switch_type] */ union
{
/* [case()][string] */ LPWSTR pOleStr;
/* [case()] */ UINT uOffset;
/* [case()] */ char cStr[ 260 ];
} DUMMYUNIONNAME;
} STRRET;
// winnt.h
typedef _Null_terminated_ WCHAR *NWPSTR, *LPWSTR, *PWSTR;
typedef LPWSTR PTSTR, LPTSTR;
Use QString::fromUcs2 to convert strings.
This is woking fine
QString str("ddddd");
LPCTSTR lstr = (LPCTSTR)str.data();

Resources