0

I need to get ASCII value from each QString's elements, then replace it with others ASCII. I have these QString: QString s = ui->lineEdit_3->text(); and QString k = ui->lineEdit_2->text();, then i converted s to Latin1 s.at(i).toLatin1(); but when i print f, nothing happens.
This is my code:

QString s = ui->lineEdit_3->text();
QString k = ui->lineEdit_2->text();
QString f;
k.toInt();
s.toStdString();


for(int i; i<=s.length(); i++)
{
    f.append(QChar(s.at(i).toLatin1()+k.toInt()));
}

ui->lineEdit->setText(f);

Thank you in advance!

GabryLinux
  • 155
  • 3
  • 8
  • 1
    `i<=s.length();` is not right. It needs be `i – R Sahu Aug 01 '18 at 17:53
  • What do you want to do? Feel free to use pseudocode. Describe what you want the behavior to be first. I'm guessing that you want to add a constant value to each element's ASCII code? – Kuba hasn't forgotten Monica Aug 01 '18 at 18:22
  • Please turn on compiler warnings, and fix them. You can do this by adding `QMAKE_CXXFLAGS += -Wall -Wextra` to your *.pro* file (assuming you have *qmake* project and are using Linux, Mac or Windows/MinGW). – hyde Aug 01 '18 at 19:23
  • 1
    After you have warnings on, compiler should warn that `i` is uninitialized. Fix that, as a starter. Then you'll want to check what `k.toInt()` actually returns (try `qDebug() << k.toInt();`) – hyde Aug 01 '18 at 19:26

1 Answers1

0

The example below demonstrates one solution. The Offset control determines by how much is the character code offset from the original value. Conversion to ASCII is unnecessary: Unicode code points correspond to ASCII where the ranges of the two overlap (i.e. 32 to 127, inclusive). The Modulo printable ASCII range option keeps the results within the printable ASCII range by performing addition modulo the range (i.e. the results are wrapped to fit in the range).

screenshot

// https://github.com/KubaO/stackoverflown/tree/master/questions/ascii-add-ui-51639347
#include <QtWidgets>

int main(int argc, char *argv[]) {
   QApplication a(argc, argv);
   QWidget ui;
   QFormLayout layout(&ui);
   QLineEdit input("Hello, World!");
   QLabel output;
   QLineEdit offset;
   QIntValidator offsetValidator(-65535, 65535);
   QCheckBox ascii("Modulo printable ASCII range 32-127");
   output.setFrameShape(QFrame::Panel);
   layout.addRow("Input Text", &input);
   layout.addRow("Output", &output);
   layout.addRow("Offset", &offset);
   layout.addRow(&ascii);
   layout.setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
   offset.setValidator(&offsetValidator);
   offset.setPlaceholderText("0");
   ascii.setChecked(true);
   ui.show();

   auto const calculate = [&] {
      bool limit = ascii.isChecked();
      auto text = input.text();
      int delta = offset.text().toInt();
      for (QChar &ch : text) {
         auto doLimit = limit && ch >= 32 && ch <= 127;
         ch = {ch.unicode() + delta};
         if (doLimit) ch = {((ch.unicode() - 32) % (128 - 32)) + 32};
      }
      output.setText(text);
   };
   for (auto ed : {&input, &offset})
      QObject::connect(ed, &QLineEdit::textChanged, calculate);
   QObject::connect(&ascii, &QCheckBox::toggled, calculate);

   offset.setText("1");
   return a.exec();
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313