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. How to best convert QString with comma to 4byteQstring hex
Forum Updated to NodeBB v4.3 + New Features

How to best convert QString with comma to 4byteQstring hex

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 836 Views 2 Watching
  • 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.
  • E Offline
    E Offline
    erytcg
    wrote on last edited by erytcg
    #1

    Hello I want to convert display text to QString 4 byte hex for example:

    QString input ->"119.1"
    float ->119.1
    int -> 1191
    hex int -> 4A7
    QString -> "4A7"
    QString output -> "04A7" always 4 byte.

    bool ok;
        int dec0 = (input.toFloat(&ok))*10;
        //QString dec1 = QString::number(dec0);
        QString c = QString::number(dec0, 16);
        if(c.size()<4) c = QString(4-c.size(), '0')+c;
    

    but when I put 29.3 output = 0124 not 0125.
    @edit with .toDouble work great but it's good way?

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Casting to int is always rounding down. If you want to round to nearest integer use qRound or add 0.5 before casting.

      QString in = "119.1";
      QString out  = QString::number(qRound(in.toFloat() * 10.0), 16).toUpper().rightJustified(4, '0');
      
      1 Reply Last reply
      3
      • E Offline
        E Offline
        erytcg
        wrote on last edited by
        #3

        thank's work's perfectly!! :D

        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