gprime
gprime

Reputation: 2353

Array to Hex Representation

I am writing a program that needs to take an array of size n and convert that into it's hex value as follows:

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

I would like to take each value of the array to represent it as binary and convert it to a hex value. In this case:

0x6000000000000000; // 0110...0

it also has to be packed to the right with 0's to be 64 bits (i am on a 64 bit machine).

Or i could also take the array elements, convert to decimal and convert to hexadecimal it that's easier... What you be the best way of doing this in C++?

(this is not homework)

Upvotes: 2

Views: 1916

Answers (5)

MSN
MSN

Reputation: 54594

unsigned long long answer= 0;
for (int i= 0; i<sizeof(a)/sizeof(a[0]); ++i)
{
    answer= (answer << 1) | a[i];
}

answer<<= (64 - sizeof(a)/sizeof(a[0]));

Assumptions: a[] is at most 64 entries, is defined at compile time, and only contains 1 or 0. Being defined at compile time sidesteps issues of shifting left by 64, as you cannot declare an empty array.

Upvotes: 1

jay.lee
jay.lee

Reputation: 19837

The following assumes that your a[] will only ever use 0 and 1 to represent bits. You'll also need to specify the array length, sizeof(a)/sizeof(int) can be used in this case, but not for heap allocated arrays. Also, result will need to be a 64bit integer type.

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

If you want to see what it looks like in hex, you can use (s)printf( "%I64x", result )

Upvotes: 4

Potatoswatter
Potatoswatter

Reputation: 137800

std::bitset<64>::to_ulong() might be your friend. The order will probably be backwards (it is unspecified, but typically index 3 will be fetched by right-shifting the word by 3 and masking with 1), but you can remedy that by subtracting the desired index from 63.

#include <bitset>

std::bitset<64> bits;

for ( int index = 0; index < sizeof a/sizeof *a, ++ index ) {
    bits[ 63 - index ] = a[ index ];
}

std::cout << std::hex << std::setw(64) << std::setfill('0')
          << bits.to_ulong() << std::endl;

Upvotes: 1

Hannesh
Hannesh

Reputation: 7488

byte hexValues[16];

for(int i = 15; i >= 0; i--) { hexValues = a[i*4] * 8 + a[i*4-1] * 4 + [i*4-2] * 2 + a[i*4-3]; }

This will give you an array of bytes where each byte represents one of your hex values.

Note that each byte in hexValues will be a value from 0 to 16.

Upvotes: 0

abelenky
abelenky

Reputation: 64682

Here's a rough answer:

int ConvertBitArrayToInt64(int a[])
{
    int answer = 0;

    for(int i=0; i<64; ++i)
    {
        if (isValidIndex(i))
        {
            answer = answer << 1 | a[i];
        }
        else
        {
            answer = answer << 1;
        }
    }
    return answer;
}

Upvotes: 0

Related Questions