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. ASCII number to Byte.
Forum Updated to NodeBB v4.3 + New Features

ASCII number to Byte.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 797 Views 1 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.
  • D Offline
    D Offline
    ddlandim
    wrote on 10 Jul 2020, 15:42 last edited by
    #1

    Hello, this is my first post on forum.
    Here is what i have to do:
    i have a QString. example: "EARTH"
    the i need take every char, one by one, convert to ascii number, multiply the number by 2, convert back again to ascii, and build a byte array to send over tcpsocket.

    rules:
    this bytearray is for a ax25 header, his lenght is always 6. if i receive some string smaller then 6, i have to fill the rest with "".

    my first try was:
    build a byteArray_out filled with 0x40 (thats represents "" ).

    1. do a loop (at the satId.at.size() representing the "in" data) and take the chars from satId, and convert to ascii number, finally multiplying by 2 (ok).
      int v = (satId.at(i).toLatin1())*2;

    2. build a bytearray from this number, take the first byte of this bytearray, and write in byteArray_out position at the loop (didnt work)

    byteArray_out [i] = QByteArray::number(v, 16)[0];

    I build a code taking another way (but i belive is not the better way){

    int AX25Header::Initialize(QString satId, QString callsign)
    {
        if(satId.size() > 6 || callsign.size() > 6)
            return -1;
        //converting formula = string to ASCII decimal, decimal X2, new decimal to hex.
        //0x40 = ""
    
        QString satIdx2;
        for(int i=0; i < 6; i++){
            if(i < satId.size()){
                int v = satId.at(i).toLatin1();
                v = v*2;
                QString hexadecimal; hexadecimal.setNum(v,16);
                satIdx2.append(hexadecimal);
            }
            else
                satIdx2.append("40");
        }
        QByteArray satId_ba = QByteArray::fromHex(satIdx2.toUtf8());
        if(satId_ba.size()!=6){
            log("destination callsign size error");
            return -1;
        }
        m_Instance.setDestinationCallSign(satId_ba);
        m_Instance.setDestinationSSID(0x00);
        QString satId_ba_str = satId_ba.toHex();
        log("TC AX25 Destination Callsign = " + satId_ba_str);
    
        QString callsignx2;
        for(int i=0; i < 6; i++){
            if(i < callsign.size()){
                int v = callsign.at(i).toLatin1();
                v = v*2;
                QString hexadecimal; hexadecimal.setNum(v,16);
                callsignx2.append(hexadecimal);
            }
            else
                callsignx2.append("40");
        }
        QByteArray callsignx2_ba = QByteArray::fromHex(callsignx2.toUtf8());
        if(callsignx2_ba.size()!=6){
            log("source callsign size error");
            return -1;
        }
        m_Instance.setSourceCallSign(callsignx2_ba);
        m_Instance.setSourceSSID(0x00);
        QString callsign_ba_str = callsignx2_ba.toHex();
        log("TC AX25 Source Callsign = " + callsign_ba_str);
    
        // AX25 Control Fields
        m_Instance.setAX25ControlField(0x03);
        m_Instance.setAX25PIDField(0xF0);
    
        return 0;
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Jul 2020, 16:51 last edited by
      #2

      Hi and welcome to devnet,

      First thing to do: use only QByteArray. QString stores your string in UTF16 and you are doing useless back and forth transformation.

      What do you mean by ASCII number ? Every char has a numerical representation so I am not sure what you want to achieve here.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 10 Jul 2020, 17:56
      0
      • S SGaist
        10 Jul 2020, 16:51

        Hi and welcome to devnet,

        First thing to do: use only QByteArray. QString stores your string in UTF16 and you are doing useless back and forth transformation.

        What do you mean by ASCII number ? Every char has a numerical representation so I am not sure what you want to achieve here.

        D Offline
        D Offline
        ddlandim
        wrote on 10 Jul 2020, 17:56 last edited by
        #3

        @SGaist said in ASCII number to Byte.:

        Every char has a numerical representation so I am not sure what you want to achieve here.

        hi! thanks for reply!
        ill give you a didatic example.

        i have this string:
        N C B R 2

        i have to convert every char to a ascii number

        N = 78
        C = 76
        B = 66
        R = 82
        2 = 50
        "" = 32

        then i have to multiply the ascii numbers by 2:

        78 -> 156
        132 -> 134
        66 -> 132
        82 -> 50
        50 -> 100
        32 -> 64

        and finally convert the new numbers to hex:

        156 -> 9c
        132 -> 86
        132 -> 84
        164 -> a4
        100 -> 64
        64 -> 40

        so my final QByteArray is "9c-86-84-a4-64-40"

        Look this table, the conversion is down to up.
        3048da77-a2bd-42af-8b13-9fa622fc5986-image.png

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 10 Jul 2020, 19:19 last edited by
          #4

          When you've only ascii characters in your string then convert your QString to a QByteArray and iterate over the single chars. Converting an integer back to a QString / QByteArray can be done with QString/QByteArray::number()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          1/4

          10 Jul 2020, 15:42

          • Login

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