I have an ugly code for this stuff (create a c char pointer and copy the QString in it) but maybe ... exist in QT an elegant way ...
actual code :
QString maquina is a method parameter.
char *c_maquina = new char[maquina.length() + 1];
strcpy(c_maquina, maquina.toStdString().c_str());
just for information I need a REAL char* not a simple const char* so this code not work :
idMaquina.toLatin1().data();
I can't use http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa
This is simple:
QByteArray array = string.toLocal8Bit();
char* buffer = array.data();
You can also use toLatin1 or toUtf8 instead of toLocal8Bit. Note that neither of them can be queued with data call. And toStdString().c_str() is also invalid. This is because any QByteArray or std::string produced in such a way is temporary and will be destroyed immediately destroying char buffer with it. You need to store QByteArray in a local variable while you're using the buffer.
Also note that Qt provides QByteArray class to deal with char arrays. Generally there is no need to use char*, you can do almost anything with QByteArray.
I think the solution depends on the type of the characters to be converted, and whether a C-style function with "char *" type arguments needs to be integrated/called.
If a C-style function needs to be integrated/called, do not use toStdString() followed by c_str(), as the return value type is "const char *" which is not suitable for a C-style function.
Use toLatin1() followed by data() for ASCII characters.
Use toLocal8Bit() or toUtf8() followed by data() for other UTF8 characters than ASCII ones.
If several solutions can be used for your specific case, their efficiency levels may be slightly different, which I have not tested.
The following test program shows how to use these solutions:
#include <QCoreApplication>
#include <QDebug>
// This is a C-style test function which needs an argument of type "char *":
void my_c_func(char * my_c_str)
{
printf(" my_c_str[%s]\n", my_c_str);
}
// This is a program which tests the conversion from "QString" to "char *":
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Case 1: ASCII characters
// ========================
QString qString1 = "French";
qDebug().nospace().noquote() << "qString1[" << qString1 << "]"; // qString1[French]
// Solution 1.1: to Latin1 QByteArray, followed by data() in 2 steps:
QByteArray latin1BAString1 = qString1.toLatin1();
char * latin1_c_str1 = latin1BAString1.data();
qDebug().nospace().noquote() << "latin1_c_str1[" << latin1_c_str1 << "]"; // latin1_c_str1[French]
my_c_func(latin1_c_str1);
// Solution 1.2: to local 8-bit QByteArray, followed by data() in 2 steps:
QByteArray local8bitBAString1 = qString1.toLocal8Bit();
char * local8bit_c_str1 = local8bitBAString1.data();
qDebug().nospace().noquote() << "local8bit_c_str1[" << local8bit_c_str1 << "]"; // local8bit_c_str1[French]
my_c_func(local8bit_c_str1);
// Solution 1.3: to UTF8 QByteArray, followed by data() in 2 steps:
QByteArray utf8BAString1 = qString1.toUtf8();
char * utf8_c_str1 = utf8BAString1.data();
qDebug().nospace().noquote() << "utf8_c_str1[" << utf8_c_str1 << "]"; // utf8_c_str1[French]
my_c_func(utf8_c_str1);
// !!! Try: Solution 1.4: to std::string , followed by c_str() in 2 steps:
std::string stdString1 = qString1.toStdString();
const char * stdstring_c_str1 = stdString1.c_str(); // "const" must be used !
qDebug().nospace().noquote() << "stdstring_c_str1[" << stdstring_c_str1 << "]"; // stdstring_c_str1[French]
// invalid conversion from 'const char*' to 'char*': ---> NOT GOOD for use by a C-style function !!!
// my_c_func(stdstring_c_str1);
qDebug() << "";
// Case 2: Non-ASCII characters
// ============================
QString qString2 = "français";
qDebug().nospace().noquote() << "qString2[" << qString2 << "]"; // qString2[français]
// !!! Try: Solution 2.1: to Latin1 QByteArray, followed by data() in 2 steps:
QByteArray latin1BAString2 = qString2.toLatin1();
char * latin1_c_str2 = latin1BAString2.data();
qDebug().nospace().noquote() << "latin1_c_str2[" << latin1_c_str2 << "]"; // latin1_c_str2[fran?ais] ---> NOT GOOD for non-ASCII characters !!!
my_c_func(latin1_c_str2);
// Solution 2.2: to Local 8-bit QByteArray, followed by data() in 2 steps:
QByteArray local8bitBAString2 = qString2.toLocal8Bit();
char * local8bit_c_str2 = local8bitBAString2.data();
qDebug().nospace().noquote() << "local8bit_c_str2[" << local8bit_c_str2 << "]"; // local8bit_c_str2[français]
my_c_func(local8bit_c_str2);
// Solution 2.3: to UTF8 QByteArray, followed by data() in 2 steps:
QByteArray utf8BAString2 = qString2.toUtf8();
char * utf8_c_str2 = utf8BAString2.data();
qDebug().nospace().noquote() << "utf8_c_str2[" << utf8_c_str2 << "]"; // utf8_c_str2[français]
my_c_func(utf8_c_str2);
// !!! Try: Solution 2.4: to std::string, followed by c_str() in 2 steps:
std::string stdString2 = qString2.toStdString();
const char * stdstring_c_str2 = stdString2.c_str(); // "const" must be used !
qDebug().nospace().noquote() << "stdstring_c_str2[" << stdstring_c_str2 << "]"; // stdstring_c_str2[français]
// invalid conversion from 'const char*' to 'char*': ---> NOT GOOD for use by a C-style function !!!
// my_c_func(stdstring_c_str2);
return a.exec();
}
The above code has been tested using Qt 5.4 for Linux.
A second subject involved in this question is whether we can chain functions together during this 2-step conversion process:
<myQString>.to<AnotherClass>().<getCPointer>(); // OK or not?
I think this depends on "AnotherClass" and on the type of characters to be converted. Based on some documentation on QString, QByteArray and std::string, it appears that it is safe to write:
<myQString>.toStdString().c_str(); // OK.
<myQString>.toUtf8().data(); // Should be OK as QString is Unicode string.
But the following lines should be avoided:
<myQString>.toLocal8Bit().data(); // May crash if the converted QByteArray object is undefined !
<myQString>.toLatin1().data(); // May crash if the converted QByteArray object is undefined !
I use this in my code all the time
char * toCharP(QString in)
{
QByteArray a; a.append(in);
return a.data();
}
QString::toLatin1().data() gives you a const char* because it gives you its internal buffer. The reason it's const is because you're not supposed to modify it.
So if you want to modify it, you have to copy that data to some other buffer... such as that one you just allocated using new().
std::vector<char> result;
result.reserve( qstr.length()+1 ); // +1 might not be needed, not sure how QString counts
result.insert( result.end(), qstr.begin(), qstr.end() );
char* ptr = result.data(); // while retval exists, retval.data() is a char* pointing to a buffer
QByteArray contains a non const version of data(). See:
http://qt-project.org/doc/qt-5.0/qtcore/qbytearray.html#data
Sometimes, there's just no way to keep your code at top beauty. Deal with it. You might wrap it in a little helper function, taking QString in parameter and returning char*, if you really want.
Related
What is the most basic way to do it?
If compiled with STL compatibility, QString has a static method to convert a std::string to a QString:
std::string str = "abc";
QString qstr = QString::fromStdString(str);
If by string you mean std::string you can do it with this method:
QString QString::fromStdString(const std::string & str)
std::string str = "Hello world";
QString qstr = QString::fromStdString(str);
If by string you mean Ascii encoded const char * then you can use this method:
QString QString::fromAscii(const char * str, int size = -1)
const char* str = "Hello world";
QString qstr = QString::fromAscii(str);
If you have const char * encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use this method:
QString QString::fromLocal8Bit(const char * str, int size = -1)
const char* str = "zażółć gęślą jaźń"; // latin2 source file and system encoding
QString qstr = QString::fromLocal8Bit(str);
If you have const char * that's UTF8 encoded then you'll need to use this method:
QString QString::fromUtf8(const char * str, int size = -1)
const char* str = read_raw("hello.txt"); // assuming hello.txt is UTF8 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const char*
QString qstr = QString::fromUtf8(str);
There's also method for const ushort * containing UTF16 encoded string:
QString QString::fromUtf16(const ushort * unicode, int size = -1)
const ushort* str = read_raw("hello.txt"); // assuming hello.txt is UTF16 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const ushort*
QString qstr = QString::fromUtf16(str);
Alternative way:
std::string s = "This is an STL string";
QString qs = QString::fromAscii(s.data(), s.size());
This has the advantage of not using .c_str() which might cause the std::string to copy itself in case there is no place to add the '\0' at the end.
std::string s = "Sambuca";
QString q = s.c_str();
Warning: This won't work if the std::string contains \0s.
I came across this question because I had a problem when following the answers, so I post my solution here.
The above examples all show samples with strings containing only ASCII values, in which case everything works fine. However, when dealing with strings in Windows whcih can also contain other characters, like german umlauts, then these solutions don't work
The only code that gives correct results in such cases is
std::string s = "Übernahme";
QString q = QString::fromLocal8Bit(s.c_str());
If you don't have to deal with such strings, then the above answers will work fine.
Do you mean a C string, as in a char* string, or a C++ std::string object?
Either way, you use the same constructor, as documented in the QT reference:
Qt QString Reference
For a regular C string, just use the main constructor:
char name[] = "Stack Overflow";
QString qname(name);
For a std::string, you obtain the char* to the buffer and pass that to the QString constructor:
std::string name2("Stack Overflow");
QString qname2(name2.c_str());
Moreover, to convert whatever you want, you can use the QVariant class.
for example:
std::string str("hello !");
qDebug() << QVariant(str.c_str()).toString();
int test = 10;
double titi = 5.42;
qDebug() << QVariant(test).toString();
qDebug() << QVariant(titi).toString();
qDebug() << QVariant(titi).toInt();
output
"hello !"
"10"
"5.42"
5
I need help regarding rgb covertor in the hex code. I am trying to make a function return hex code. I need cString to be hex. For import I use :
dwTitleColor1 // Red
dwTitleColor2 // Green
dwTitleColor3 // Blue
const char * CHARACTER::GetTitleColor() const
{
static char cString[CHARACTER_NAME_MAX_LEN + 1];
dwTitleColor1 = 0
dwTitleColor2 = 0
dwTitleColor3 = 0
snprintf(cString, sizeof(cString), "r:%d, g:%d, b:%d.", dwTitleColor1, dwTitleColor2, dwTitleColor3);
return cString;
}
Why don't you use C++ tools?
std::string CHARACTER::GetTitleColor() const
{
std::ostringstream buffer;
buffer.flags(std::ios_base::hex | std::ios_base::left);
buffer.fill('0');
buffer <<"r: " <<std::setw(2) <<dwTitleColor1
<<", g: " <<std::setw(2) <<dwTitleColor2
<<", b: " <<std::setw(2) <<dwTitleColor3;
return buffer.str();
}
This will write each color as a2-digit hex number. Adapt formatting at will: drop the flags if you want decimal, remove the setw and fill if you don't need leading 0.
(And rename that class, you don't want to use all-caps for anything but macros in a C++ program).
[edit]
Since it seems to cause a bit of confusion, I want to state that I purposefully changed the return type to be a std::string. Because in C++ strings are std::string, not char*. Its use is very simple:
// Assuming myChar is a CHARACTER instance
std::string colorA = myChar.GetTitleColor(); // straightforward
auto colorB = myChar.GetTitleColor(); // better, color gets automatic type from method return type
const auto & colorC = myChar.GetTitleColor(); // if we won't modify it, even better.
You can use the returned string however you like. You don't have to free it. It remains valid until it goes out of scope (as opposed to your static char* which gets overwritten if you call GetTitleColor on another character).
And if you really have no other choice, you can always do the same thing you were doing with a static: replace the return line with those two:
static std::string result = buffer.str();
return result.c_str();
It has the exact same caveat your static version had though: calling GetTitleColor() again will make the previously returned pointer invalid.
This should do it:
snprintf(cString, sizeof(cString), "r:%x, g:%x, b:%x.",
dwTitleColor1, dwTitleColor2, dwTitleColor3);
In my app I read a string field from a file in local (not Unicode) charset.
The field is a 10 bytes, the remainder is filled with zeros if the string < 10 bytes.
char str ="STRING\0\0\0\0"; // that was read from file
QByteArray fieldArr(str,10); // fieldArr now is STRING\000\000\000\000
fieldArr = fieldArr.trimmed() // from some reason array still containts zeros
QTextCodec *textCodec = QTextCodec::codecForLocale();
QString field = textCodec->ToUnicode(fieldArr).trimmed(); // also not removes zeros
So my question - how can I remove trailing zeros from a string?
P.S. I see zeros in "Local and Expressions" window while debuging
I'm going to assume that str is supposed to be char const * instead of char.
Just don't go over QByteArray -- QTextCodec can handle a C string, and it ends with the first null byte:
QString field = textCodec->toUnicode(str).trimmed();
Addendum: Since the string might not be zero-terminated, adding storage for a null byte to the end seems to be impossible, and making a copy to prepare for making a copy seems wasteful, I suggest calculating the length ourselves and using the toUnicode overload that accepts a char pointer and a length.
std::find is good for this, since it returns the ending iterator of the given range if an element is not found in it. This makes special-case handling unnecessary:
QString field = textCodec->toUnicode(str, std::find(str, str + 10, '\0') - str).trimmed();
Does this work for you?
#include <QDebug>
#include <QByteArray>
int main()
{
char str[] = "STRING\0\0\0\0";
auto ba = QByteArray::fromRawData(str, 10);
qDebug() << ba.trimmed(); // does not work
qDebug() << ba.simplified(); // does not work
auto index = ba.indexOf('\0');
if (index != -1)
ba.truncate(index);
qDebug() << ba;
return 0;
}
Using fromRawData() saves an extra copy. Make sure that the str
stays around until you delete the ba.
indexOf() is safe even if you have filled the whole str since
QByteArray knows you only have 10 bytes you can safely access. It
won't touch 11th or later. No buffer overrun.
Once you removed extra \0, it's trivial to convert to a QString.
You can truncate the string after the first \0:
char * str = "STRING\0\0\0\0"; // Assuming that was read from file
QString field(str); // field == "STRING\0\0\0\0"
field.truncate(field.indexOf(QChar::Null)); // field == "STRING" (without '\0' at the end)
I would do it like this:
char* str = "STRING\0\0\0\0";
QByteArray fieldArr;
for(quint32 i = 0; i < 10; i++)
{
if(str[i] != '\0')
{
fieldArr.append(str[i]);
}
}
QString can be constructed from a char array pointer using fromLocal8Bit. The codec is chosen the same way you do manually in your code.
You need to set the length manually to 10 since you say you have no guarantee that an terminating null byte is present.
Then you can use remove() to get rid of all null bytes. Caution: STRI\0\0\0\0NG will also result in STRING but you said that this does not happen.
char *str = "STRING\0\0\0\0"; // that was read from file
QString field = QString::fromLocal8Bit(str, 10);
field.remove(QChar::Null);
i have a buffer
char buffer[size];
which i am using to store the file contents of a stream(suppose pStream here)
HRESULT hr = pStream->Read(buffer, size, &cbRead );
now i have all the contents of this stream in buffer which is of size(suppose size here). now i know that i have two strings
"<!doctortype html" and ".html>"
which are present somewhere (we don't their loctions) inside the stored contents of this buffer and i want to store just the contents of the buffer from the location
"<!doctortype html" to another string ".html>"
in to another buffer2[SizeWeDontKnow] yet.
How to do that ??? (actually contents from these two location are the contents of a html file and i want to store the contents of only html file present in this buffer). any ideas how to do that ??
You can use strnstr function to find the right position in your buffer. After you've found the starting and ending tag, you can extract the text inbetween using strncpy, or use it in place if the performance is an issue.
You can calculate needed size from the positions of the tags and the length of the first tag nLength = nPosEnd - nPosStart - nStartTagLength
Look for HTML parsers for C/C++.
Another way is to have a char pointer from the start of the buffer and then check each char there after. See if it follows your requirement.
If that's the only operation which operates on HTML code in your app, then you could use the solution I provided below (you can also test it online - here). However, if you are going to do some more complicated parsing, then I suggest using some external library.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
const char* beforePrefix = "asdfasdfasdfasdf";
const char* prefix = "<!doctortype html";
const char* suffix = ".html>";
const char* postSuffix = "asdasdasd";
unsigned size = 1024;
char buf[size];
sprintf(buf, "%s%sTHE STRING YOU WANT TO GET%s%s", beforePrefix, prefix, suffix, postSuffix);
cout << "Before: " << buf << endl;
const char* firstOccurenceOfPrefixPtr = strstr(buf, prefix);
const char* firstOccurenceOfSuffixPtr = strstr(buf, suffix);
if (firstOccurenceOfPrefixPtr && firstOccurenceOfSuffixPtr)
{
unsigned textLen = (unsigned)(firstOccurenceOfSuffixPtr - firstOccurenceOfPrefixPtr - strlen(prefix));
char newBuf[size];
strncpy(newBuf, firstOccurenceOfPrefixPtr + strlen(prefix), textLen);
newBuf[textLen] = 0;
cout << "After: " << newBuf << endl;
}
return 0;
}
EDIT
I get it now :). You should use strstr to find the first occurence of the prefix then. I edited the code above, and updated the link.
Are you limited to C, or can you use C++?
In the C library reference there are plenty of useful ways of tokenising strings and comparing for matches (string.h):
http://www.cplusplus.com/reference/cstring/
Using C++ I would do the following (using buffer and size variables from your code):
// copy char array to std::string
std::string text(buffer, buffer + size);
// define what we're looking for
std::string begin_text("<!doctortype html");
std::string end_text(".html>");
// find the start and end of the text we need to extract
size_t begin_pos = text.find(begin_text) + begin_text.length();
size_t end_pos = text.find(end_text);
// create a substring from the positions
std::string extract = text.substr(begin_pos,end_pos);
// test that we got the extract
std::cout << extract << std::endl;
If you need C string compatibility you can use:
char* tmp = extract.c_str();
I am doing a simple client implementation of TFTP.
Here i need to send read request in following format
/* send request
2 bytes string 1 byte string 1 byte
------------------------------------------------------
RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
WRQ -------------------------------------------------
*/
in between i have to insert 1 byte zero bits value .
But i am not able to add that value.
Also if i add a 1 zero bits byte.. which actually means a string terminating character
than how to get proper strlen value.
If any one can help me with this...
enter code here
const char opcode_read[2] ={'0','1'};
const char opcode_write[2] ={'0','2'};
const char opcode_data[2] ={'0','3'};
const char opcode_acknowledge[2] ={'0','4'};
const char opcode_error[2] ={'0','5'};
const char mode_netascii[] = "netascii\0";
char blk_read_request[100];
char file_name[] = "rfc0791.txt\0";
memcpy(blk_read_request, opcode_read, 2);
memcpy(&blk_read_request[2], file_name, strlen(file_name) + 1);
memcpy(&blk_read_request[2 + strlen(file_name)], mode_netascii, strlen(mode_netascii) + 1);
for (int i = 0; i < strlen(blk_read_request); i++) {
cout << i << " : " << blk_read_request[i] << " " << std::bitset<CHAR_BIT > (blk_read_request[i]) << "\n";
}
If you want to transfer the null across, not only must you memcpy with "strlen(filename) + 1", but you'll also need to update the destination pointer for the subsequent memcpys accordingly.
memcpy(&blk_read_request[2 + strlen(file_name) + 1], mode_netascii, strlen(mode_netascii) + 1);
Note the extra +1 in there.
For the record, being C++, you might want want to consider using a higher-level class than "char *" which can handle embedded null characters.
For example, the following prints "8", and opens up std::stringstream as a better way of forming your packets:
#include <string>
#include <iostream>
int main()
{
std::string x = "foo";
x += '\0';
x += "bar";
x += '\0';
std::cout << x.length() << std::endl;
}
This is automatic in C++ strings.
This:
const char mode_netascii[] = "netascii";
Is equivalent to this:
const char mode_netascii[] = { 'n', 'e', 't', 'a', 's', 'c', 'i', 'i', '\0' };
If you need to deal with data that will (or might) include null bytes then you'll generally want to avoid using C-style strings and the functions that deal with them. Use character/byte arrays and keep a length associated with the length of the data in the buffers.
In C++ vectors of bytes (or char) would work great for this.
Also in C++, the std::string type can contain null characters just fine; however, I'd suggest avoiding their use and sticking with std::vector<char> since it would be all too easy to fall into some bug traps of passing the result of string::c_str() to something that expects null terminated strings.
char *total_send_string = malloc(2+strlen(file_name)+sizeof(Mode)+3);
memset(total_send_string, 0, strlen(total_send_string);
//Then copy each member of the total send packet into the correct offset
//Now you have the correct result