Convert char array into int in C -


say have char array, holds 8 bytes. how convert char array integer?

i tried using sscanf -

int x; sscanf(char_array, "%d", &x); 

i'm reading bytes binary file, storing them char array, , i'm trying print out int value based on offset value.

the following converts 4 byte array (4 chars) 32-bit unsigned integer. should able extend 8 chars (i.e., 64-bit unsigned int).

iterate array backwards (can forwards well) , shift int representation of respective character accordingly , fit resultant value.

#include <iostream> #include <cstdint>  using namespace std;  int main() {     char arr[] = {0x00, 0x00, 0x1b, 0x1b}; // testing convenience     uint32_t val = 0;     (int = 3; >= 0; i--) {         uint32_t tmp = arr[i];         int j = 4 - i;         while (--j) {             tmp <<= 8;         }         val |= tmp;     }      cout << val << endl; } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -