0

I am trying to compare a specific character in a QString, but getting odd results:

My QString named strModified contains: "[y]£trainstrip+[height]£trainstrip+8"

I convert the string to a standard string:

    std:string stdstr = strModified.toStdString();

I can see in the debugger that 'stdstr' contins the correct contents, but when I attempt to extract a character:

    char cCheck = stdstr.c_str()[3];

I get something completely different, I expected to see '£' but instead I get -62. I realise that '£' is outside of the ASCII character set and has a code of 156.

But what is it returning?

I've modified the original code to simplify, now:

    const QChar cCheck = strModified.at(intClB + 1);

    if ( cCheck == mccAttrMacroDelimited ) {
       ...
    }

Where mccAttrMacroDelimited is defined as:

   const QChar clsXMLnode::mccAttrMacroDelimiter = '£';

In the debugger when looking at both definitions of what should be the same value, I get:

   cCheck: -93 '£'
   mccAttrMacroDelimiter:  -93 with what looks like a chinese character

The comparison fails...what is going on?

I've gone through my code changing all QChar references to unsigned char, now I get a warning:

    large integer implicitly truncated to unsigned type [-Woverflow]

on:

    const unsigned char clsXMLnode::mcucAttrMacroDelimiter = '£';

Again, why? According to the google search this may be a bogus message.

SPlatten
  • 5,334
  • 11
  • 57
  • 128

2 Answers2

0

I am happy to say that this has fixed the problem, the solution, declare check character as unsigned char and use:

    const char cCheck = strModified.at(intClB + 1).toLatin1();
SPlatten
  • 5,334
  • 11
  • 57
  • 128
0

I think because '£' is not is the ASCII table, you will get weird behavior from 'char'. The compiler in Xcode does not even let me compile

char c = '£'; error-> character too large for enclosing literal type

You could use unicode since '£' can be found on the Unicode character table £ : u+00A3 | Dec: 163.

The answer to this question heavily inspired the code I wrote to extract the decimal value for '£'.

#include <iostream>
#include <codecvt>
#include <locale>
#include <string>

using namespace std;

//this takes the character at [index] and prints the unicode decimal value
u32string foo(std::string const & utf8str, int index)
{
  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
  std::u32string utf32str = conv.from_bytes(utf8str);

  char32_t u = utf32str[index];
  cout << u << endl;
  return &u;
}

int main(int argc, const char * argv[]) {
  string r = "[y]£trainstrip+[height]£trainstrip+8";
  
  //compare the characters at indices 3 and 23 since they are the same
  cout << (foo(r,3) == foo(r, 23)) << endl;

  return 0;
}

You can use a for loop to get all of the characters in the string if you want. Hopefully this helps

Community
  • 1
  • 1
Duly Kinsky
  • 996
  • 9
  • 11