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.
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.
Have a look at QByteArray::toInt
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?
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:If the example makes sense is another story, but I am simply interpreting whatever ba[i] returns as an integer.Qt Code:
int sum = 0; // Sum up the ascii values of "Hello" for( int i = 0; i < ba.size(); i++ ) { sum += ba[i]; }To copy to clipboard, switch view to plain text mode
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.
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
You cauld convert the element to int first.
something like -
Qt Code:
int value = ba.at(i); if(value == 255) ...To copy to clipboard, switch view to plain text mode
hope you get the idea
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
it wont fail for those values..would u care to show us some of ur relevant code snippet?
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]
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()
{
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 :
and you can get data:
or
now you need to construct the integer from it. For example something like this:
I dont know if its the best way but it's a code snippet from one of my old projects and it was working :]Qt Code:
qint32 result = 0; for (int i = 0; i < 4; ++i) result |= ((qint32)data[i] & 0x000000ff) << (i * 8);To copy to clipboard, switch view to plain text mode
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):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
SIFE (9th June 2011)
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....");
}
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...
May be you can try the following too-
1.QByteArray::toUIntorQt Code:
int value = b.toUint();To copy to clipboard, switch view to plain text mode
2.
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
I was a bit confused by QByteArray::QByteArray ( const char * str )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
Havent you ever come across a assignment likefor 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!..
You are right int is 4/8 bytes and char is 1 byte. However how you convert and use, depends on you.Qt Code:
char ch; switch(ch) { 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 ? } or int a = 'a';To copy to clipboard, switch view to plain text mode
Lets do a walkthrough...method that faldzip posted is correct one for reading integers.
Qt Code:
qint32 result = 0; for (int i = 0; i < 4; ++i) 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:
result = ((qint32)data[i] & 0x000000ff)To copy to clipboard, switch view to plain text mode
Ofcourse, thats why I had given the conversion quint32 value = ba.at(i);.but in this case, i guess kewlcode is reading characters and interpreting them as integers
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. You can guess data was QByteArray data = buffer.readAll();Qt Code:
const uchar c = uchar(data[currpos]);To copy to clipboard, switch view to plain text mode
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!
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):
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:
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:
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:
and so on...
at the end we get:
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):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
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 ?
Through the network, data is always in network byte order,I.e. Big endian
Bookmarks