I need to get the decimal value of a 4 byte unsigned char array like
0xEADE105B.

I figured I'd convert each byte separately and add them together, like so:

__int64 dec_val = 0xEA * 0x01000000;

Why is it that this expression yields -369098752 instead of 3925868544 ?

Thanks

Re: need a 32 bit decimal number by Victor

Victor
Fri Oct 10 10:03:54 CDT 2008

Dave Cullen wrote:
> I need to get the decimal value of a 4 byte unsigned char array like
> 0xEADE105B.
>
> I figured I'd convert each byte separately and add them together, like so:
>
> __int64 dec_val = 0xEA * 0x01000000;
>
> Why is it that this expression yields -369098752 instead of 3925868544 ?

Because the top bit is set (by extension) and it is the sign bit in
__int64... Perhaps you should declare 'dec_val' unsigned __int64?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Re: need a 32 bit decimal number by Carl

Carl
Fri Oct 10 10:14:43 CDT 2008

Victor Bazarov wrote:
> Dave Cullen wrote:
>> I need to get the decimal value of a 4 byte unsigned char array like
>> 0xEADE105B.
>>
>> I figured I'd convert each byte separately and add them together,
>> like so: __int64 dec_val = 0xEA * 0x01000000;
>>
>> Why is it that this expression yields -369098752 instead of 3925868544 ?
>
> Because the top bit is set (by extension) and it is the sign bit in
> __int64... Perhaps you should declare 'dec_val' unsigned __int64?
>

The top bit in a 64-bit integer is not set in that number, but the top bit
in a 32-bit number is. The problem is that the math is all done using
ints - casting the constant multipliers to unsigned int or to __int64 will
yield the correct answer.

-cd



Re: need a 32 bit decimal number by Dave

Dave
Sat Oct 11 07:58:00 CDT 2008

> The top bit in a 64-bit integer is not set in that number, but the top bit
> in a 32-bit number is. The problem is that the math is all done using
> ints - casting the constant multipliers to unsigned int or to __int64 will
> yield the correct answer.
>
> -cd

Thanks, that worked.

drc



Re: need a 32 bit decimal number by Cezary

Cezary
Sat Oct 11 11:59:21 CDT 2008

Hello,

>> The top bit in a 64-bit integer is not set in that number, but the top bit
>> in a 32-bit number is. The problem is that the math is all done using
>> ints - casting the constant multipliers to unsigned int or to __int64 will
>> yield the correct answer.

> Thanks, that worked.

Simply add ,,U'' postfix to a constant:

__int64 dec_val = 0xEA * 0x01000000U;

Arguments of binary expressions are converted to the most ,,capacious''
type, but at least to int. Unsigned integral types are considered to be
wider then signed counterparts. Then we have: (signed => unsigned) *
unsigned, which produces unsigned int. Otherwise we will have int * int,
which produces int regardless of overflow. In the first case we have
unsigned to __int64 conversion by assignment operator and zero extended
MSB. In the second case we have signed (negative with MSB set) to
__int64 conversion and sign extended MSB which, in effect, produces
negative ,,dec_val''.

-- best regards

Cezary Noweta