QChar to uint32_t conversion / casting in Qt

1.6k views Asked by At

I have a QString of 1500 QChar which I want to convert to an array of uint32_t. I am reading each element in for loop and trying to save each QChar of QString to uint32_t array. I can convert it to its equivalent representation (e.g. 1 -> 49) through data[i].unicode() but I want the same string in an uint32_t array form for further processing.

QString data = {1,'A','C',9,8,.....};
uint32_t Test[data.length()] = {0};
for (uint32_t i =0; i<data.length(); i++) {
    Test[i] = data[i]; // here i need QChar to uint32_t conversion
}

Any solutions?

2

There are 2 answers

0
vahancho On

Taking into account that each QChar holds only 16 bits and consist of two parts: cell and row, you can even pack two QChars into a single uint32_t number. I would do it in the following way:

QString data("ABCDE");
std::vector<uint32_t> test((data.count() + 1) / 2, 0);

for (int i = 0; i < data.length(); ++i)
{
    const QChar &c = data[i];
    uchar cell = c.cell();
    uchar row = c.row();

    uint32_t &ui32 = test[i / 2];
    if (i % 2 == 0)
    {
        ui32 = cell;
    }
    else
    {
        ui32 <<= 8;
        ui32 |= cell;
    }
    ui32 <<= 8;
    ui32 |= row;
}

However if you still need to have a single integer number per QChar you can write similar loop without compression like:

QString data("ABCDE");    
std::vector<uint32_t> test(data.count(), 0);

for (int i = 0; i < data.length(); ++i)
{
    const QChar &c = data[i];
    uchar cell = c.cell();
    uchar row = c.row();

    uint32_t &ui32 = test[i];
    ui32 = cell;
    ui32 <<= 8;
    ui32 |= row;
}
0
Kuba hasn't forgotten Monica On

I want to store items of QString data in uint32_t Test[] array. E.g. If I have QString data = {1,'A','C',9,8,.....} as input than at output I want to have uint32_t Test = {1,'A','C',9,8,.....}

It's not complicated at all. Each QChar is a thin wrapper around uint16_t. All you need to do is to convert those to uint32_t and you're done.

QVector<uint32_t> convert(const QString & str) {
  QVector<uint32_t> output;
  output.reserve(str.size());
  for (auto c : str)
    output.append(c.unicode());
  return output;
}

void user(const uint32_t *, size_t size);

void test(const QString & str) {
  auto const data = convert(str);
  user(data.data(), size_t(data.size());
}

Of course it might be that you have wrong assumptions about the meaning of uint32_t. The code above assumes that the user expects UTF16 code units. It's more likely, though, that user expects UTF32 code units, i.e. each uint32_t represents exactly one Unicode code point.

In the latter case, you have to convert conjugate pairs of UTF16 code units to single UTF32 code units:

QVector<uint32_t> convert(const QString & str) {
  return str.toUcs4();
}

Note that code point and code unit have specific meanings and are not synonymous.