PDA

View Full Version : Converting u_char to QString



merlvingian
28th September 2006, 02:43
Hello,

I have searched the forums here and could not find an answer and what I could find on the Internet was run through translators destroying meaning.

I need to convert a (const u_char *varname) into a QString and then back again. I scoured the Qt4 docs and could not find anything relating to a u_char or conversion method.

What I could translate from some forums was to use reinterpret_cast<> but could not implement it correctly in this situation.

Any help would be appreciated.

wysota
28th September 2006, 06:09
QString string = QString((const char*)varname); // directly from the string
QString string = QString::fromLocal8Bit((const char*)varname); // or using local encoding

merlvingian
28th September 2006, 15:25
I guess I am a newbie :D Sorry for posting in the wrong section.

Thank you for the response and will try it out once I get back to a machine where I can access Qt. But from the look of the code should it not throw an error on char to u_char conversion?

I guess my question then evolves into using depriciated c style type casting in a c++ program. QString seems very flexible does anyone know why such trickery is needed for something as common as a uchar?

wysota
28th September 2006, 15:38
I guess I am a newbie :D Sorry for posting in the wrong section.
No problem.


But from the look of the code should it not throw an error on char to u_char conversion?
No. u_char is unsigned char, so casting u_char* to const char* is perfectly legitimate.


I guess my question then evolves into using depriciated c style type casting in a c++ program. QString seems very flexible does anyone know why such trickery is needed for something as common as a uchar?
Probably the conversion between u_char* and const char* is not handled by your compiler automatically.

And it's not that common to hold strings in u_char*. char* (or const char*) is the most straightforward container for character strings.

merlvingian
28th September 2006, 16:02
Thank you again for the reply.

Couldn't wait so I switched to a machine I could test this out on.

The problem I see now is as follows:

The u_char data I am converting to a QString is network data is why I need the u_char 0-225 etc etc. My guess is the char typecast is not going to lose any accuaracy?

When I access the QString with .toAscii or .toLatin1 I get an output typical of an improperly index array.

What I am looking to accomplish is taking network data and storing it in an xml file. I must be able to convert it to a QString for storage and *should* be able to read it back as already formatted QString text when needed to display.

Am I accessing the QString wrong? Is there a conversion I am missing?

wysota
28th September 2006, 16:16
Is the network data a character string? Or some custom data? Because if the latter, there is no sense using QString for that as QString is suited for characters (i.e. it uses Unicode). If you just want a dynamic array, use QByteArray or QVector.

merlvingian
28th September 2006, 16:23
Is the network data a character string? Or some custom data? Because if the latter, there is no sense using QString for that as QString is suited for characters (i.e. it uses Unicode). If you just want a dynamic array, use QByteArray or QVector.

The network data is from LibPcap (winpcap at this point). It appeared that QByteArray was better suited for this situation by I am learning as I go with Qt.

Just recently finished C++ GUI programming with Qt4, and desgin patterns in Qt4 but I am still very new to this and I appriciate the help you have provided greatly.

merlvingian
29th September 2006, 00:11
Thank you again Wysota,

I spent so much time trying to out think the problem in Qt I lost sight of what I was trying to accomplish :D



QString str;

for (int i=0;i<lengthofthenetworkdataarray;i++)
str += QString::number(somepacketdata[i],16);



Did exactly what I needed. Thank you again for taking the time to help a newbie.