Results 1 to 7 of 7

Thread: Hex to Ascii and Ascii to Hex Problem

  1. #1
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Hex to Ascii and Ascii to Hex Problem

    hi all,
    This is my code:

    #include <QCoreApplication>
    #include <QDebug>
    QString HexToAscii(QString Str);
    QString AsciiToHex(QString Str);

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    qDebug() << HexToAscii("4170706c65");
    qDebug() << AsciiToHex(HexToAscii("4170706c65").toLocal8Bit()) ;

    qDebug() << HexToAscii("410070706c65"); // "00" is added after "41"
    qDebug() << AsciiToHex(HexToAscii("410070706c65").toLocal8Bit( ));

    return a.exec();
    }


    QString HexToAscii(QString String)

    {

    QByteArray ByteArray1=String.toUtf8();

    const char* chArr1=ByteArray1.constData();

    QByteArray ByteArray2=QByteArray::fromHex(chArr1);

    const char* chArr2=ByteArray2.constData();

    return QString::fromUtf8(chArr2);

    }


    QString AsciiToHex(QString String)

    {

    QByteArray ByteArray1=String.toUtf8();

    QByteArray ByteArray2=ByteArray1.toHex();

    const char* chArr1=ByteArray2.constData();

    return QString::fromUtf8(chArr1);

    }

    And the Output is:
    "Apple"
    "4170706c65"
    "A"
    "41"


    why the "00" is causing the problem there? I want the last line to be "410070706c65"
    Sorry but I'm very new to Qt, please help me with the solution and the reason behind the unexpected value.
    thanks in advance.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Hex to Ascii and Ascii to Hex Problem

    Please specify the platform/Qt Veriosn/ABI (32/64/... bit) information.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Hex to Ascii and Ascii to Hex Problem

    Quote Originally Posted by havoc View Post
    why the "00" is causing the problem there? I want the last line to be "410070706c65"
    Sorry but I'm very new to Qt, please help me with the solution and the reason behind the unexpected value.
    thanks in advance.
    C-strings are terminated by NUL (i.e. '\0') character (nothing to do with Qt). Feeding an array of bytes contain zeroes to any function expecting a C-string (i.e. const char *) will behave this way: this includes the Qt function QString::fromUtf8().

    QByteArray is the Qt data type that handles arrays of arbitrary bytes. Your HexToAscii() function is what QByteArray::fromHex() does. Your AsciiToHex() is precisely what QByteArray::toHex() does. All the other stuff you do in those functions is unnecessary and ultimately breaks your expectations.

  4. #4
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Hex to Ascii and Ascii to Hex Problem

    thanks you for your responses. ChrisW67, can you show me an example on how I can deal with the NUL? I am basically wanting it for a program which writes(using socket->write() ) the data containing NUL somewhere in the middle of the string, often many NULs inbetween. How can I manage it?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Hex to Ascii and Ascii to Hex Problem

    You use the version of QIODevice::write() that takes a QByteArray or a const char * with a size, and not the version that expects a nul terminated c string. None of this has anything to do with converting bytes to or from hexadecimal.

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Hex to Ascii and Ascii to Hex Problem

    Thank you, I'll look onto that. Actually the packet data I am trying to send is like the ASCII form of "02000204690001000001" and its hard for me to directly send the packet in ASCII form so I was trying to store the packet in hexadecimal form in a variable and use it at the time of sending by converting it to ASCII using the HexToAscii converter and later after receiving the bytes in ASCII form I want to display them in Hex form using the AsciiToHex converter. I have done this easily in Visual Basic 6 and I recently started learning Qt so I'm facing too many problems. I searched many times still found nothing. I don't know if I'm asking for too much, but can you give me a clue or any link where I can learn this from?

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Hex to Ascii and Ascii to Hex Problem

    Actually the packet data I am trying to send is like the ASCII form of "02000204690001000001"
    What do you mean "the ASCII form of" in relation to a perfectly good ASCII string?

    I think you more urgently need to understand what hexadecimal is: it is a human-readable representation of a number in base 16. A hexadecimal string is an ASCII string representing a collection of bytes that may not have values valid in an ASCII string, i.e. any byte value over 127, or inconvenient in a string like zero bytes.


    Assuming that string is in a QString variable s:
    Qt Code:
    1. QByteArray buf = s.toLatin1();
    2. iodev.write( buf );
    To copy to clipboard, switch view to plain text mode 
    Will send that string through the device. That is a series of 20 bytes with values (decimal): 48 50 48 48 48 50 etc.

    If you mean the string is the hexadecimal representation of a series of 10 bytes then:
    Qt Code:
    1. QByteArray buf = QByteArray::fromHex( s.toLatin1() );
    2. iodev.write( buf );
    To copy to clipboard, switch view to plain text mode 
    Will write ten bytes with the values (decimal): 2 0 2 4 105 0 1 0 0 1

    There is no need to convert binary data to hexadecimal strings in Qt unless you are doing it to display to a human.

  8. The following user says thank you to ChrisW67 for this useful post:

    havoc (11th August 2013)

Similar Threads

  1. Replies: 8
    Last Post: 14th April 2010, 05:54
  2. ASCII to int and int to ASCII
    By askbapi in forum Newbie
    Replies: 2
    Last Post: 31st March 2010, 22:36
  3. Decoding extended ascii
    By waynew in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2010, 12:26
  4. ASCII to PDF
    By jaca in forum Newbie
    Replies: 3
    Last Post: 19th September 2009, 23:51
  5. Ascii problem
    By eekhoorn12 in forum Qt Programming
    Replies: 5
    Last Post: 17th October 2007, 13:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.