Results 1 to 20 of 20

Thread: Read ByteArray

  1. #1
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Read ByteArray

    I have a ByteArray . I want to read the content and know its equivalent no. e.g.

    if bytearray has 255 as its first member then i want to read the first member and the value (i.e. 255) assigned to an integer.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    Have a look at QByteArray::toInt

  3. #3
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    Thanks Aamer for replying.

    I am confused by QByteArray::toInt . Does it return one integer or an array of integers? The bytearray consists of many bytes. How do i convert all of them to integers?

  4. #4
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    I don't see your problem .
    If you access an element of the QByteArray by at() or with operator [] you get a "char". And a char is a signed 8 bit value.

    So it is not necessary to convert the QByteArray to an array of integers as the elements can simply be used as if they were integers

    Example:
    Qt Code:
    1. QByteArray ba("Hello");
    2. int sum = 0;
    3.  
    4. // Sum up the ascii values of "Hello"
    5. for( int i = 0; i < ba.size(); i++ )
    6. {
    7. sum += ba[i];
    8. }
    To copy to clipboard, switch view to plain text mode 
    If the example makes sense is another story, but I am simply interpreting whatever ba[i] returns as an integer.

    Concerning the toInt() method:
    This method tries to return an integer value represented by the bytes in the QByteArray.
    As the example in the Qt docs shows if the QByteArray is "FF" and you call array.toInt(&ok, 16); You want the array to be considered as hexadecimal value. So toInt() returns the integer value 255.
    If the array is "FZ" and you call array.toInt(&ok, 16); the method tells you with ok == false that "FZ" is not a hex value.

    So with toInt() you can "make" an integer out of an array of bytes. Just like QString::toInt() would make it. Difference is that QString uses unicode characters an QByteArray holds bytes (chars).

    Confused enough ?
    Last edited by Boron; 4th April 2009 at 16:50.

  5. #5
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    Boron,
    thanks for your reply.

    Had checked out the "FF" example in the docs. It is fine if it is just one byte i.e. FF.
    My byte array suppose contains bytes like -> FF 2 3 25 10 etc. How do I check if the value is FF or 10 etc. ?
    An example which failed ->


    for( int i = 0; i < ba.size(); i++ )
    {
    if (ba[i] == 255){
    //pte1->appendPlainText (".......OK....");
    }
    }



    This gives the error

    C:/Users/vinay/Prog/telnetpro/telnetpro/telnetpro.cpp:61: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:

    for the line if (ba[i] == 255){



    tia

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    You cauld convert the element to int first.

    something like -
    Qt Code:
    1. int value = ba.at(i);
    2. if(value == 255)
    3. ...
    To copy to clipboard, switch view to plain text mode 

    hope you get the idea

  7. #7
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    Aamer,

    Thx .

    What you suggested works for smaller values but not for higher values in the byte array. I have a suspicion it fails for values above 127 in the byte array.

    Still

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    it wont fail for those values..would u care to show us some of ur relevant code snippet?

  9. #9
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    I am trying to connect to a telnet host. I have a simple form with 'pte1' plaintextedit box and a 'pb1' pushbutton. When I click pb1 pushbutton it establishes connection to host 10.10.1.10 and read the data that arrives on the TcpSocket telcli. The telnet protocol sends some data which includes byte '255' etc.
    In the below code it never prints OK on the plaintextedit box.
    I am using
    pte1->appendPlainText (QString::number(value));

    to examine the value of the integer and it prints -1 , -5 etc for (I beleive) values higher than 127.

    [FONT=Courier New]#include "telnetpro.h"[/FONT]
    [FONT=Courier New]#include "ui_telnetpro.h"[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]#include <QtGui>[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]telnetpro::telnetpro(QWidget *parent)[/FONT]
    [FONT=Courier New]: QWidget(parent), ui(new Ui::telnetproClass)[/FONT]
    [FONT=Courier New]{[/FONT]
    [FONT=Courier New]setupUi(this);[/FONT]
    [FONT=Courier New]connect( pb1, SIGNAL( clicked()),this, SLOT(starttel() ) );[/FONT]
    [FONT=Courier New]connect( &telcli, SIGNAL( readyRead() ),this, SLOT(readtel() ) );[/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]void telnetpro::starttel()[/FONT]
    [FONT=Courier New]{[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]telcli.connectToHost("10.10.1.10",23);[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]void telnetpro::readtel()[/FONT]
    [FONT=Courier New]{[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]QByteArray ba;[/FONT]
    [FONT=Courier New]int i ;[/FONT]
    [FONT=Courier New]int value;[/FONT]
    [FONT=Courier New]ba = telcli.readAll();[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]pte1->appendPlainText ("BEGIN");[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]for( int i = 0; i < ba.size(); i++ )[/FONT]
    [FONT=Courier New]{[/FONT]
    [FONT=Courier New]value = ba.at(i);[/FONT]
    [FONT=Courier New]if (value > 200){[/FONT]
    [FONT=Courier New]pte1->appendPlainText (".......OK....");[/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New]pte1->appendPlainText (QString::number(value));[/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]pte1->appendPlainText (".......OVER....");[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New][/FONT]
    [FONT=Courier New]telnetpro::~telnetpro()[/FONT]
    [FONT=Courier New]{[/FONT]
    [FONT=Courier New]telcli.close();[/FONT]
    [FONT=Courier New]delete ui;[/FONT]
    [FONT=Courier New]}[/FONT]
    [FONT=Courier New][/FONT]

  10. #10
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    Sorry the above post is a bit messed up. Posting again the same thing.




    I am trying to connect to a telnet host. I have a simple form with 'pte1' plaintextedit box and a 'pb1' pushbutton. When I click pb1 pushbutton it establishes connection to host 10.10.1.10 and read the data that arrives on the TcpSocket telcli. The telnet protocol sends some data which includes byte '255' etc.
    In the below code it never prints OK on the plaintextedit box.
    I am using
    pte1->appendPlainText (QString::number(value));

    to examine the value of the integer and it prints -1 , -5 etc for (I beleive) values higher than 127.


    #include "telnetpro.h"
    #include "ui_telnetpro.h"

    #include <QtGui>

    telnetpro::telnetpro(QWidget *parent)
    : QWidget(parent), ui(new Ui::telnetproClass)
    {
    setupUi(this);
    connect( pb1, SIGNAL( clicked()),this, SLOT(starttel() ) );
    connect( &telcli, SIGNAL( readyRead() ),this, SLOT(readtel() ) );
    }

    void telnetpro::starttel()
    {

  11. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    If you want just to print the telnet output just append QString made of QByteArray.

    If you want to get integers from QByteArray then its more complicated. For example when you get some data as QByteArray from Q*Socket, and that data contains some ints, then you need to know some additional info. The length of a integer is usual 32-bit which gives 4 bytes. Then that int can be stored as little-endian or big-endian - which describes the order of that 4 bytes:

    Hex int can be :
    Qt Code:
    1. someInt = 0xAABBCCDD;
    To copy to clipboard, switch view to plain text mode 
    and you can get data:
    Qt Code:
    1. AA BB CC DD
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. DD CC BB AA
    To copy to clipboard, switch view to plain text mode 
    now you need to construct the integer from it. For example something like this:
    Qt Code:
    1. qint32 result = 0;
    2. for (int i = 0; i < 4; ++i)
    3. result |= ((qint32)data[i] & 0x000000ff) << (i * 8);
    To copy to clipboard, switch view to plain text mode 
    I dont know if its the best way but it's a code snippet from one of my old projects and it was working :]

    P.S. And use CODE tags ([#] button) for pasting code.
    Last edited by faldzip; 5th April 2009 at 00:11.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. The following user says thank you to faldzip for this useful post:

    SIFE (9th June 2011)

  13. #12
    Join Date
    Apr 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read ByteArray

    Finally it worked. I think I was getting -ve numbers for values more than 127 e.g. I would get -1 for 255. When I added 256 to any -ve values it is ok .

    Thanks to all of you.


    void telnetpro::readtel()
    {

    QByteArray ba;
    int i ;
    int value , val1;
    ba = telcli.readAll();
    pte1->appendPlainText ("BEGIN");

    for( int i = 0; i < ba.size(); i++ )
    {
    value = ba.at(i);
    if (value < 0) {
    val1 = value + 256;
    }
    else {
    val1=value;
    }
    if (val1 > 200){
    pte1->appendPlainText (".......OK....");
    }
    pte1->appendPlainText (QString::number(val1));
    }

    pte1->appendPlainText (".......OVER....");

    }

  14. #13
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    so were u trying to read characters or integers?cuz the right way to read integers over internet or through a bytearray is how faldżip explained...

  15. #14
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    May be you can try the following too-
    1.QByteArray::toUInt
    Qt Code:
    1. QByteArray b = ba.at(i);
    2. int value = b.toUint();
    To copy to clipboard, switch view to plain text mode 
    or
    2.
    Qt Code:
    1. quint32 value = ba.at(i);
    To copy to clipboard, switch view to plain text mode 

  16. #15
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    Quote Originally Posted by aamer4yu View Post
    May be you can try the following too-
    1.QByteArray::toUInt
    Qt Code:
    1. QByteArray b = ba.at(i);
    2. int value = b.toUint();
    To copy to clipboard, switch view to plain text mode 
    or
    2.
    Qt Code:
    1. quint32 value = ba.at(i);
    To copy to clipboard, switch view to plain text mode 
    i dont think those methods are gonna be useful if u r trying to find an int..for the first method, at() returns a char..u cant convert a char into QByteArray..no such constructor for QByteArray..for the second method, again its just converting one char to whole int..which is wrong when one knows the whole value of an integer is in 4/8 bytes depending on the architecture, not just one byte!..method that faldzip posted is correct one for reading integers..but in this case, i guess kewlcode is reading characters and interpreting them as integers

  17. #16
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    i dont think those methods are gonna be useful if u r trying to find an int..for the first method, at() returns a char..u cant convert a char into QByteArray..no such constructor for QByteArray
    I was a bit confused by QByteArray::QByteArray ( const char * str )
    for the second method, again its just converting one char to whole int..which is wrong when one knows the whole value of an integer is in 4/8 bytes depending on the architecture, not just one byte!..
    Havent you ever come across a assignment like
    Qt Code:
    1. char ch;
    2. switch(ch)
    3. { case 'a' : // case is evaluated in terms of int, right ? So dont you think an internal conversion from char to int is being done ? Is it wrong way ?
    4. }
    5. or int a = 'a';
    To copy to clipboard, switch view to plain text mode 
    You are right int is 4/8 bytes and char is 1 byte. However how you convert and use, depends on you.
    method that faldzip posted is correct one for reading integers.
    Lets do a walkthrough...
    Qt Code:
    1. qint32 result = 0;
    2. for (int i = 0; i < 4; ++i)
    3. result |= ((qint32)data[i] & 0x000000ff) << (i * 8);
    To copy to clipboard, switch view to plain text mode 

    he too is converting data[i] to qint32 , ie., integer !!. The success of above code lies in the mask. It will just cut off the negative signature.
    Lets say we '01' in data[i]. After typecast, it gets converted to '0000 0001'. Pay heed to the shift number (i*8). What happens if you shift '0000 0001' eight times ? You get the same number !! Even say '0000 0012' you will get '0000 0012'. And since you are doing it in multiples of eight, it wont matter how many times you do it. !!
    So why is it working ? (qint32)data[i] & 0x000000ff is the answer. I guess the whole loop sums down to
    Qt Code:
    1. result = ((qint32)data[i] & 0x000000ff)
    To copy to clipboard, switch view to plain text mode 

    .but in this case, i guess kewlcode is reading characters and interpreting them as integers
    Ofcourse, thats why I had given the conversion quint32 value = ba.at(i);
    By the way, for telnet purposes, it will be better to use unsigned values. If you have Qt Solutions, you might find qttelnet example. Even there they have used uchar
    Heres the line
    Qt Code:
    1. const uchar c = uchar(data[currpos]);
    To copy to clipboard, switch view to plain text mode 
    . You can guess data was QByteArray data = buffer.readAll();

  18. #17
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    hey all i was trying to say is when one end, i.e. telnet here, is sending an integer, it is sending 4 bytes to represent a particular value..so its just a bit weird to just read one byte and say we read integer..either the other end is sending bytes which are to be interpreted as integers or chars..here u r interpreting it as integer but still reading them as char and converting them to integers..thats all my point..nothing to aggravate u cheers!

  19. #18
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    Quote Originally Posted by aamer4yu View Post
    Lets do a walkthrough...
    Qt Code:
    1. qint32 result = 0;
    2. for (int i = 0; i < 4; ++i)
    3. result |= ((qint32)data[i] & 0x000000ff) << (i * 8);
    To copy to clipboard, switch view to plain text mode 

    he too is converting data[i] to qint32 , ie., integer !!. The success of above code lies in the mask. It will just cut off the negative signature.
    Lets say we '01' in data[i]. After typecast, it gets converted to '0000 0001'. Pay heed to the shift number (i*8). What happens if you shift '0000 0001' eight times ? You get the same number !! Even say '0000 0012' you will get '0000 0012'. And since you are doing it in multiples of eight, it wont matter how many times you do it. !!
    So why is it working ? (qint32)data[i] & 0x000000ff is the answer. I guess the whole loop sums down to
    Qt Code:
    1. result = ((qint32)data[i] & 0x000000ff)
    To copy to clipboard, switch view to plain text mode 
    Are '0000 0001' and '0000 0012' in HEX? It looks like hex cause then it gives 4 bytes. So if it's in hex then what is a result of shifting '0000 0012' eight times? The answer is '0000 1200' so it's not the same number, it's multiplied by 2^8 = 256. So do the walkthrough again. Here is what we get in data (bytes written with HEX):
    Qt Code:
    1. AA BB CC DD
    To copy to clipboard, switch view to plain text mode 
    now we have a result = 0; which is in hex = 00 00 00 00;
    then we have a for loop (4 rounds):
    1. we take AA and convert it to the qint32:
    Qt Code:
    1. AA => 00 00 00 AA
    To copy to clipboard, switch view to plain text mode 
    then we cut the first byte with 00 00 00 FF to cut out the negative FFs (for byte negative values converted to int), then shifting it 0*8 = no shift :]
    We have now:
    Qt Code:
    1. result == 00 00 00 AA;
    To copy to clipboard, switch view to plain text mode 
    2. almost the same but before logical sum (|=) we shift the '00 00 00 BB' by 8 so we get '00 00 BB 00', after logical sum with '00 00 00 AA' we get:
    Qt Code:
    1. result == 00 00 BB AA;
    To copy to clipboard, switch view to plain text mode 
    and so on...
    at the end we get:
    Qt Code:
    1. result == DD CC BB AA;
    To copy to clipboard, switch view to plain text mode 
    so we constructed one 4 byte integer from 4 bytes (normally interpreted as chars) sotred in QByteArray. :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  20. #19
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    My Mistake !!
    You know I was sleeping before and after that post I had made. Now when I woke up, I realised I was writing in terms of hex, and still using only a byte !! thats why the confusion

    By the way, few questions..
    I guess you are assuming an int will take 4 indexes in bytearray ?
    And if the original number was 0xAABBCCDD, the result is 0xDDCCBBAA, isnt it ? What endian or protocol are u assuming while sending and receiving data ?

  21. #20
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read ByteArray

    Through the network, data is always in network byte order,I.e. Big endian

Similar Threads

  1. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 07:38
  2. External Lib read from QBuffer on Calback problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2008, 19:43
  3. How to read Raw Information from CD in MAC?
    By vishal.chauhan in forum General Programming
    Replies: 0
    Last Post: 10th July 2007, 12:26
  4. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  5. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

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.