View Full Version : help to convert code from c++ to qt
ruben.rodrigues
11th May 2011, 12:42
Hi all!
I have a function that was given to me from a c++ project and I have to use it in qt. The function receives a byte value and according to that byte value creates a bitarray with true and false values that represent flags.
Lets say that I have 7 flags {cancel, retry, confirm, stop, manual, resume, skip}
Then for example the value:
- 1 means that the cancel is active.
- 2 means that the retry is active.
- 2 means that the cancel and the retry are active.
- 4 means that the confirm is active.
- 5 means that the cancel and the confirm are active
...
...
and so on until 127 where all are active
a co-worker of mine wrote a function to do this in C++ but I can use it well in Qt:
private BitArray ToBitArray(byte b)
{
BitArray ba = new BitArray(8);
for (int i = 7; i >= 0; i--) {
if (b >= Convert.ToByte(Math.Pow(2d, (double)i))) {
ba[i] = true;
b = Convert.ToByte(b - Math.Pow(2d, (double)i));
} else {
ba[i] = false;
}
}
return ba;
}
so far in qt I have this:
int flags = 6;
QBitArray ba; ba.resize(8);
for (int i = 7; i >= 0; i--){
if(flags >= int(pow(double(i),2))){
ba[i] = true;
flags = int(flags - pow(double(i),2));
}else{
ba[i] = false;
}
}
for (int i = 0; i < ba.size(); i++){
if(ba[i]){
cout << i << ": true" << endl;
}else {
cout << i << ": false" << endl;
}
}
I get some true and false correct but most of them are wrong.
FelixB
11th May 2011, 12:47
why do you have top convert the code? Qt is C++. you can use c++-code in any qt-project (ok, except pyqt).
ruben.rodrigues
11th May 2011, 13:06
why do you have top convert the code? Qt is C++. you can use c++-code in any qt-project (ok, except pyqt).
I can't use the BitArray so I have to use the QBitArray, I can't use the byte variable type and the Convert.ToByte function. What do I have to include to use those variables and functions??
rsilva
11th May 2011, 13:26
Ruben, that code that have BitArray isn't C++ code, it's C# (C-Sharp) code.
A byte is a unsigned char, so. Instead of using int flags, replace to unsigned char flags.
Note:
unsigned char = 1 byte = 8 bits
int (or unsigned int) = 4 byte = 32 bits
int flags = 6;
to
unsigned char flags = 6;
Now you need to convert this flags = int(flags - pow(double(i),2)); to byte, right ?
As the type is just an unsigned char, try this:
flags = (unsigned char)(flags - pow(double(i), 2));
Note: If you don't need for check for big-endian computers I think it will work, and if you want this works for all system, I think you will need to do some research on this, it's simple ^^
(Someone corrects me if this works in big-endian computers)
If you want to read more:
http://en.wikipedia.org/wiki/Endianness
ruben.rodrigues
11th May 2011, 13:35
thanks for your time.
You are right, the code is C#. when I use the (byte) typecasting, the compliler gives me this error @ flags = (byte)(flags - pow(double(i), 2)); // ‘byte’ was not declared in this scope
what can I do about that?
rsilva
11th May 2011, 13:40
I've just fixed it in my post:
flags = (unsigned char)(flags - pow(double(i), 2));
or you can just declare tye type too ^^:
typedef unsigned char byte;
ruben.rodrigues
11th May 2011, 13:53
thanks for your time. I can compile the code again but it is still not working properly. Maybe the C# code was already wrong.
For ex. the flags = 4 should give me the ba[0] = false, the ba[1] = false and the ba[2] = true. Although it tell me that all first 3 are true. The same with flags = 2, should give the only the ba[1] as true but it gives me the ba[0] = true as well.
Isn't there already a working function available? I looked up but didn't find anything.
szisziszilvi
11th May 2011, 13:54
what the others are also telling is that there is no data type called "byte" in c++.
Instead you can use char or unsigned char, just check this out:http://cplusplus.com/doc/tutorial/variables/
Anyway if you would like to use something just slightly little bit more intuitive you may like uint8_t in stdint.h which is available under qt, but is not that "world-wide", for example DevC++ does not recognise, nor does the simple gcc which I tried once under a Linux distro. As you can see on page http://linux.die.net/include/stdint.h it is not really anything more than some typedefs, but it is convenient in a way. But let me point out also again that it is not surely the best solution as this header is not obviously given. But its web page gives at least a hint which typedef gives what sort of variable. :)
rsilva
11th May 2011, 13:55
You want to convert the flags to an array ?
If you're trying to do this, I think using the AND or OR operators may be better.
http://www.cprogramming.com/tutorial/bitwise_operators.html
ruben.rodrigues
11th May 2011, 14:18
You want to convert the flags to an array ?
If you're trying to do this, I think using the AND or OR operators may be better.
http://www.cprogramming.com/tutorial/bitwise_operators.html
I am trying to create a bitarray with boolean values and allegedly the C# code already does that.
The flags variable represents the sum of the available flags and those are the following:
Cancel = 0x0000001
Retry = 0x0000002
Confirm = 0x0000004
Stop = 0x0000008
Manual = 0x0000010
Resume = 0x0000020
Skip = 0x0000040
Then for ex.:
- 1 is only cancel.
- 2 is only retry.
- 3 is cancel/retry.
- 4 is only confirm
....
Although when I use the flag 4 it gives me cancel/confirm ... and for example the 2 give me cancel/retry
Is there already an example of my code idea? I looked up but didn't find it.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.