Results 1 to 8 of 8

Thread: [Solved] QHostAddress and QByteArray question

  1. #1
    Join Date
    May 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Solved] QHostAddress and QByteArray question

    Hello,
    Here come piece of code

    Qt Code:
    1. ba = sock->read(4);
    2. QHostAddress addr((quint32)ba.constData());
    3. thisHost = addr.toString();
    To copy to clipboard, switch view to plain text mode 

    Any idea why if ba contain 174.194.35.191 ( google IP for example ) addr is 11.25.48.176? What is wrong here?
    Last edited by RossenD; 30th May 2012 at 15:11.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHostAddress and QByteArray question

    Network/host byte order mismatch?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QHostAddress and QByteArray question

    Because constData() returns a char * that you cast to quint32 and the QHostAddress thinks it's an ip address, though it's just the memory address of a char.

  4. #4
    Join Date
    May 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHostAddress and QByteArray question

    Quote Originally Posted by Cruz View Post
    Because constData() returns a char * that you cast to quint32 and the QHostAddress thinks it's an ip address, though it's just the memory address of a char.
    You mind that cast actually cast char * value not char *?

    Quote Originally Posted by wysota View Post
    Network/host byte order mismatch?
    Hm...To swap bytes before cast?

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QHostAddress and QByteArray question

    Qt Code:
    1. quint32 bad = (quint32)ba.constData();
    To copy to clipboard, switch view to plain text mode 

    bad now contains the physical memory address of the first byte of the QByteArray ba. It's not the content of the first byte, it's the address of the first byte.
    If you pass this as an argument like QHostAddress(bad), then you have created a network address from a memory address. It is of course not the address, that ba contains.

  6. #6
    Join Date
    May 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHostAddress and QByteArray question

    Quote Originally Posted by Cruz View Post
    It's not the content of the first byte, it's the address of the first byte.
    Ok, so how to cast content, not address of the first byte?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHostAddress and QByteArray question

    What does the byte array actually contain? What exactly did you write to the socket?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    May 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default re: QHostAddress and QByteArray question

    Case solved

    Qt Code:
    1. quint32 uiAddress;
    2. memcpy( &uiAddress, ba.constData(), 4);
    3. QHostAddress addr( uiAddress);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QByteArray,QDataStream Question
    By aash_89 in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2010, 22:40
  2. Question on QByteArray::QHttp::readAll()
    By ask8y@yahoo.com in forum Qt Programming
    Replies: 2
    Last Post: 17th June 2010, 16:51
  3. a question regarding QByteArray
    By franco.amato in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2010, 23:50
  4. QHostAddress
    By rjschirmer in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 23:13
  5. QHostAddress::toString
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2008, 17:53

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.