Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UTF-8 Code error
QtWS25 Last Chance

UTF-8 Code error

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 932 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • freddy311082F Offline
    freddy311082F Offline
    freddy311082
    wrote on last edited by
    #1

    Hi guys

    I'm building an app for spanish language and I have some trouble reading a txt file one i have some characters like á,é,í,ó,ú, Á, É,Í,Ó,Ú, Ñ....

    my code is this one:

    @
    QFile file("/home/freddy/probar");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&file);
    in.setCodec("UTF-8"); // change the file codec to UTF-8.

    while(!in.atEnd())
    {
    QString line = in.readLine();
    qDebug() << line.toLocal8Bit(); // convert to locale multi-byte string
    }
    exit(0);
    @

    with this example, when in the pace of any some character exposed above, i get this one: �

    after that, I create some new file with this content: áááááéééÁÁÁÁÁÁÑÑÑÑññññññ

    and the code work fine.. I'm thinking that the first file is not coded in UTF-8...

    so, what can I do to get the correct characters from my first file ???

    I'm using Archlinux, Qt5.2.1, my OS language is English and my locale command give me this output:

    @LANG=en_US.UTF-8
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER="en_US.UTF-8"
    LC_NAME="en_US.UTF-8"
    LC_ADDRESS="en_US.UTF-8"
    LC_TELEPHONE="en_US.UTF-8"
    LC_MEASUREMENT="en_US.UTF-8"
    LC_IDENTIFICATION="en_US.UTF-8"
    LC_ALL=@

    best regards

    freddy

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      Since you think your first file is not UTF-8 encoded, then check that in order to be sure and then figure out which is the used encoding.
      Or since your locale settings are using UTF-8, just convert your first file into UTF-8, for instance using the "C-x C-m f" in emacs. If this file was obtained by copy-paste from a web page or from a pdf, it might contain special characters which prevent the UTF-8 encoding to be used.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved