I am trying to convert an unsigned long long to QString by using QString::number(). But it's giving me the following error "call of overloaded 'number(long long unsigned int*&)' is ambiguous. Can anyone help me?
EDIT
GetBoardSN(0, SN);
ui->tableWidget_Ethernet->setItem(0,2,new QTableWidgetItem(QString::number(SN)));
Header file :
int GetBoardSN(int instance, unsigned long long *SN);
Your SN seems to be a pointer (unsigned long long *). Otherwise you would not be able to call GetBoardSN that way. So your code assumes the variable to have two different types. GetBoardSN requires SN to be a unsigned long long* pointer, String::number() requires SN to be a value for example of type unsigned long long.
To solve this, depending on your context, you can either declare SN as a non-pointer type and call GetBoardSn with a reference to this instance:
GetBoardSN(0, &SN);
ui->tableWidget_Ethernet->setItem(0,2,new QTableWidgetItem(QString::number(SN)));
or keep the pointer type and resolve the pointer before accessing its value:
GetBoardSN(0, SN);
ui->tableWidget_Ethernet->setItem(0,2,new QTableWidgetItem(QString::number(*SN)));
Which one is the better solution depends on your overall usage of SN.
Related
What is CARD32 data-type and where is it defined? I am reading xserver code and am unable to find definitions for CARD data type.
Since the comments on the post cite the wrong code snippet for justification, I wanted to give a proper answer.
As already noted CARD32 is an unsigned 32 bit integer.
The type is defined in a C header file in X11/Xmd.h (GitHub Permalink as of 8th Sept. 2021).
Here is the revelant excerpt:
# ifdef LONG64
typedef unsigned long CARD64;
typedef unsigned int CARD32;
# else
typedef unsigned long long CARD64;
typedef unsigned long CARD32;
# endif
Further up, the LONG64 macro is defined only for 64-bit architectures. So, CARD32 is a unsigned int on 64-bit architectures and unsigned long everywhere else.
I want to get the data pointer of a string variable(like string::c_str() in c++) to pass to a c function and I found this doesn't work:
package main
/*
#include <stdio.h>
void Println(const char* str) {printf("%s\n", str);}
*/
import "C"
import (
"unsafe"
)
func main() {
s := "hello"
C.Println((*C.char)(unsafe.Pointer(&(s[0]))))
}
Compile error info is: 'cannot take the address of s[0]'.
This will be OK I but I doubt it will cause unneccesary memory apllying. Is there a better way to get the data pointer?
C.Println((*C.char)(unsafe.Pointer(&([]byte(s)[0]))))
There are ways to get the underlying data from a Go string to C without copying it. It will not work as a C string because it is not a C string. Your printf will not work even if you manage to extract the pointer even if it happens to work sometimes. Go strings are not C strings. They used to be for compatibility when Go used more libc, they aren't anymore.
Just follow the cgo manual and use C.CString. If you're fighting for efficiency you'll win much more by just not using cgo because the overhead of calling into C is much bigger than allocating some memory and copying a string.
(*reflect.StringHeader)(unsafe.Pointer(&sourceTail)).Data
Strings in go are not null terminated, therefore you should always pass the Data and the Len parameter to the corresponding C functions. There is a family of functions in the C standard library to deal with this type of strings, for example if you want to format them with printf, the format specifier is %.*s instead of %s and you have to pass both, the length and the pointer in the arguments list.
In C++ there is a way to cast a char to int and get the ascii value in return. Is there such a way to do the same with a qchar? Since unicode supports so many characters and some of them are actually looking alike, it is sometimes hard to tell what one is dealing with. An explicit code point or a number that can be used to get such would be very helpful.
I searched a the web and this site for a solution but so far no luck, Qt documentation isn't much of help either, unless I'm overlooking something.
Thank you in advance!
EDIT:
Maybe I wasn't clear enough on the matter, sorry.
Here's some code:
char chChar = 'a';
cout << (int)chChar; // will output 97, not 'a'
Also, Qt allows this:
QChar ch = 'a';
if(ch == 0x61)
//...
As far as I can tell, there has to be some information relating to the unicode codepoint in the ch object. Any possibility to get it out of there?
Took some time but I found the answer: QChar has a member named QChar::unicode which returns a ushort with its code point. Just for the record.
I'm trying to read an array of unsigned shorts using the Qt API. Unfortunately, I'm not getting the desired results.
The following code
QFile in(fileName);
int len = in.size();
QDataStream d(&in);
quint16 *data = new quint16[len];
qDebug() << data[0];
qDebug() << data[1];
d >> data[0];
qDebug() << data[0];
qDebug() << data[1];
outputs
52685
52685
13109
52685
Implying that the data is only changed at the first array position. Also, I always thought that arrays are zero initialized? Using a QByteArray doesn't seem to work here, that's why I'm trying to use a array of quint16 (= unsigned shorts). Using a loop may be an option, but I'm trying to avoid a costly loop where ever possible.
So, how do fill said array (data) with the desired data from the file? Is it possible to carry the data using a QByteArray?
First of all, in.size() returns the size of the file in bytes, and since you are using unsigned shorts (which are 2 bytes each), the size of your data array should be len/2.
Also, QDataStream is provided for serialization purposes. This means that it is mainly useful for extracting single objects at a time. See the documentation for QDataStrean for more information.
You can extract the whole array without loops or copying with this code:
QFile in(fileName);
in.open(QFile::ReadOnly);
QByteArray byteArray = in.readAll();
quint16 *data = (quint16*) byteArray.data();
If you only wish to read data but never modify it, you can make your program a lot faster by changing the last line to:
const quint16* data = (const quint16*) byteArray.constData();
Keep in mind however that with this (somewhat ugly) code the pointer will only be valid for the lifetime of the QByteArray object. This usually means that you can only use data until the end of the function.
If you wish your data to persist longer than that, you must allocate the array and read directly into it:
QFile in(fileName);
in.open(QFile::ReadOnly);
int len = in.size();
quint16 *data = new quint16[len/2];
in.read((char*)data, len);
This way, you can access the data until you delete[] data.
Finally, to answer your subquestion, arrays are zero-initialized only if they are globally defined (and you shouldn't rely even on this). Arrays allocated with new or malloc are never zero-initialized for performance reasons.
If you look at the documentation for QDataStream, you'll see there are two other methods at your disposal worth considering, if you don't want to use a loop:
QDataStream::readBytes - This will allocate a char[] buffer for you. Or,
QDataStream::readRawData - This will read data into a buffer you provide.
The problem with these is they work with char (bytes), not quint16 as you desire.
I would recommend using a loop to read quint16s. It will be the most clear code. And any respectable underlying stream implementation will be reading-ahead from the hardware into a buffer, so that your many successive >> calls won't be as expensive.
I saw some there are some typedef in CPython as shown below, what does ty mean in the type name? A short form of type?
typedef struct _mod *mod_ty;
typedef struct _stmt *stmt_ty;
typedef struct _expr *expr_ty;
Given that it's a typedef and the new type is simply the structure name with _ty appended, I think you've hit the nail on the head.
It's just a short form of type so that you can instantly tell that a variable of type xyzzy_ty is simply a pointer to a variable of type struct _xyyzy.
The rules you follow for this sort of thing aren't set in stone but it's useful to be consistent.
PEP7 is the style guideline for CPython C source code, similar to PEP8 for the Python source code.