简体   繁体   中英

QChar to uint32_t conversion / casting in Qt

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 (eg 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?

Taking into account that each QChar holds only 16 bits and consist of two parts: cell and row, you can even pack two QChar s 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;
}

I want to store items of QString data in uint32_t Test[] array. Eg 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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM