简体   繁体   中英

C# Recover Int64 from negative Int32

I want to recover this long value, that was mistakenly converted to int

long longValue = 11816271602;
int intValue = (int)longValue; // gives -1068630286
long ActualLong = ?

Right shift 32 bits of ( intValue >> 32 ) gives incorrect result.

Well, the initial value

long longValue = 11816271602L; // 0x02C04DFEF2 

is five bytes long. When you cast the value to Int32 which is four bytes long

int intValue = (int)longValue; // 0xC04DFEF2 (note 1st byte 02 absence)

you inevitably lose the 1st byte and can't restore it back.

Unfortunately this is not possible. If you take a look at the binary representation you can see the reason:

10 1100 0000 0100 1101 1111 1110 1111 0010

As you can see, this number has 34-bit and not only 32-bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM