根据文档:
For most queries, the value returned by PQgetvalue is a null-terminated ASCII
string representation of the attribute value. But if PQbinaryTuples() is TRUE,
the value returned by PQgetvalue is the binary representation of the type
in the internal format of the backend server
我想PQbinaryTuples
那里是真的。
PQGetvalue()
char *
根据文档返回 a 。(uint32_t *)
会将其char *
转换为指向无符号 32 位整数的指针,*
之前将取消引用 this 以获取实际值(无符号的 32 位整数),最后ntohl
将其转换为平台的本机 32 位整数,这可能意味着原始存储格式为网络顺序。
如果我们要“拆分”该代码,那将给出:
// Get value from database as a char *
char *in_database = PQgetvalue(result, i, 0);
// Convert the pointer to char to a pointer to an unsigned, 32bit integer
uint32_t *ptr = (uint32_t *) in_database;
// Dereference that pointer to obtain the actually stored value
uint32_t stored_value = *ptr;
// Turn that value to a native integer for the CPU
uint32_t ip = ntohl(stored_value);