c++ - Array to Hex Representation -


i writing program needs take array of size n , convert it's hex value follows:

int a[] = { 0, 1, 1, 0 };

i take each value of array represent binary , convert hex value. in case:

0x6000000000000000; // 0110...0

it has packed right 0's 64 bits (i on 64 bit machine).

or take array elements, convert decimal , convert hexadecimal that's easier... best way of doing in c++?

(this not homework)

the following assumes a[] ever use 0 , 1 represent bits. you'll need specify array length, sizeof(a)/sizeof(int) can used in case, not heap allocated arrays. also, result need 64bit integer type.

for (int c=0; c<array_len; c++)   result |= a[c] << (63-c); 

if want see looks in hex, can use (s)printf( "%i64x", result )


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -