Hi all,

I'm sure this is a n00b question, but I can't seem to get my head around it!

I have a 4 BYTE array which I want to display as a decimal value.

For example, in the array it contains the hex values: 00 05 7d a4, and I would like to display 359,844.

Any clues on how I go about this?

I have tried:

BYTE freeBlocks[4];
memcpy(freeBlocks, trackInformation + 16, 5);

_tprintf(_T("%u"), strtoul((char*) freeBlocks, NULL, 16));

But this only prints out 0. I know freeBlocks contains the correct values as when I do:

for(int i; i < 4; i++)
    _tprintf(_T("%x "), freeBlocks[i]);

it displays as expected (00 05 7d a4)

Am I going about it in the right way?

Thanks,
Damien

Recommended Answers

All 2 Replies

You need to build the integer value manually using its component bytes:

#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
    BYTE freeBlocks[4] = {0x00, 0x05, 0x7d, 0xa4};
    DWORD displayValue = 0;

    displayValue = freeBlocks[3];
    displayValue |= freeBlocks[2] << 8;
    displayValue |= freeBlocks[1] << 16;
    displayValue |= freeBlocks[0] << 24;

    cout << displayValue << endl;
}

The reason strtoul() didn't work is because it uses the string representation of a number, not an array of byte values. This would do what you want, but it wouldn't solve the problem you have:

cout << strtoul("359844", 0, 10);

Your output is 0 because strtoul() is failing on invalid input.

deceptikon, thank you so much for the quick reply! Your a superstar!

Also, thanks for explaining why my version wasn't working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.