ICU_
ICU_

Reputation: 159

Convert char * to uint16 and uint32

I am using sqlite3 in C++. I am having some trouble getting the SQL results out of the char array into my struct.

class Row {
    public:
        uint8_t type;
        uint16_t user;
        uint32_t ipAddress;
        std::string name;
};

void SqlToRow(char **argv, Row *row){
    row->type = *(argv[0]);
    row->user = *(uint16_t*)argv[1];
    row->ipAddress = *(uint32_t*)argv[2];
    row->name = argv[3];
}

The only ones that are working is type and name. The rest look like random values like 892486210

Upvotes: 2

Views: 605

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726659

The error is that you are trying to re-interpret char* values as numerics. Use sscanf with SCNu16 and SCNu32, like this:

sscanf(argv[1], "%" SCNu16, &row->user);
sscanf(argv[2], "%" SCNu32, &row-> ipAddress);

Upvotes: 1

Related Questions