Proper method of appending QChar to QByteArray

Post Reply
seasoned_geek
Posts: 123
Joined: Thu Jun 11 2020 12:18 pm

Proper method of appending QChar to QByteArray

Post by seasoned_geek »

I'm having brain spasm porting (someone else's) debugger code to CopperSpice so I can have a debugger that lets me look at a CopperSpice QString. Porting by hand, not using the tool, because I don't want to be riding in that car if it goes off the rails. Well, that and this needs some restructuring, at least in the header files, so far.

There is a piece of code in this thing which works just fine in Qt land but CopperSpice land no likey.

Code: Select all

/**
 * @brief Fills in a entry from a ini-file string.
 */
int Ini::decodeValueString( IniEntry *entry, QString specialKind, QByteArray dataArray )
{
    int rc = 0;

    if ( specialKind == "ByteArray" )
    {
        QByteArray byteArray;
        char hexStr[3];
        enum {IDLE, ESC, FIRST_HEX, SECOND_HEX} state = IDLE;
        QString valueStr = dataArray;

        for ( int i = 0; i < valueStr.length(); i++ )
        {
            QChar c = valueStr[i];

            switch ( state )
            {
                case IDLE:
                {
                    if ( c == '\\' )
                    {
                        state = ESC;
                    }
                    else
                    {
                        byteArray += c;
                    }

                };

                break;
I probably need to take a long break then come back and rewrite this entire section, I also probably need to dig into the INI and see what it is really storing. If it isn't much, convert to JSON/XML.

But for now...

What is the approved hack/method of stuffing a QChar into a byte array in correct byte order?

barbara
Posts: 281
Joined: Sat Apr 04 2015 2:32 am

Re: Proper method of appending QChar to QByteArray

Post by barbara »

Porting by hand, not using the tool
We strongly suggest you do use PepperMill and then fix any code you decide should be improved. This will save you a good deal of time and energy.
Here is the question you are asking: what is the correct way to convert a QChar into a QByteArray
First of all, the QString class in CopperSpice supports UTF-8 and UTF-16 respecting the encoding. The default is UTF-8. There is no encoding used in QByteArray. The QChar class represents a single 32 bit unicode code point. This corresponds to one code point in a QString.

If you have data which may contain text beyond ASCII then you should really be using a QString and not a QByteArray.

In the case where you want to save the text to a file, then do the following:
  1. construct a QString with the QChar
  2. call QString::to
Utf8() or QString::toLatin1() which will return a QByteArray[/list]

For more information please look up the QString8 documentation.

https://www.copperspice.com/docs/cs_api/class_qstring8.html

seasoned_geek
Posts: 123
Joined: Thu Jun 11 2020 12:18 pm

Re: Proper method of appending QChar to QByteArray

Post by seasoned_geek »

Thank you for your response.
We strongly suggest you do use PepperMill and then fix any code you decide should be improved. This will save you a good deal of time and energy.
This is OpenSource. Saving time wasn't really the goal. I still have to look at everything. Having one black box magically convert another black box would put me out on a very thin twig and I'm not a small child so odds of the twig snapping...
In the case where you want to save the text to a file, then do the following:
  1. construct a QString with the QChar
  2. call QString::to
Utf8() or QString::toLatin1() which will return a QByteArray[/list]
That is what I did. I was hoping there was an "approved" method. Actually I was hoping that QByteArray had an append( QChar) method in the works as well as QChar having toLatin1() and toUtf8() in the works. Now that "characters" aren't a byte anymore, there needs to be a direct interface.

Once I get the thing functional with absolute minimum of changes, then the gutting starts. All of this code is going bye-bye. The original developer rolled their own INI file handling for "more control." (That was the answer they actually gave me.) While I think I like what they did for the roll your own syntax highlighting, the really hinky stuff pops up in the INI class and I'm not liking it. I do like the debugger though. One can debug C++, Rust, and Go.

Post Reply