Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CRC32 returns negative on big files
Message
From
01/08/2011 07:17:03
 
 
To
01/08/2011 05:49:39
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01519635
Message ID:
01519794
Views:
16
>>>>>>>>>>>>>>>(1) The short way is to cast the return result to UInteger, and change the return type of GetCrc32() to UInteger
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>IIRC you can't do that cast in VB (although you can in C# using 'unchecked')
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>Viv,
>>>>>>>>>>>>>
>>>>>>>>>>>>>I don't know, but I'd be very surprised if it weren't possible
>>>>>>>>>>>>
>>>>>>>>>>>>Just tried this which won't compile:
Dim test As UInteger = DirectCast(Int32.MaxValue, UInteger)
>>>>>>>>>>>>If it was a valid cast what resulting UInteger would you expect from a negative value signed int ?
>>>>>>>>>>>>In C#:
unchecked { UInt32 test = ((UInt32)Int32.MinValue); }
>yields 2147483648....
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>Firstly, I'm surprised that it yields an even number
>>>>>>>>>>>
>>>>>>>>>>>Secondly, if that's the case, I guess you'll need t check whether is was negative, and if so, set the left most bit after the 'cast'
>>>>>>>>>>
>>>>>>>>>>But if the signed int is negative then there is no accurate equivalent for an unsigned int ?
>>>>>>>>>>I'm assuming the C# cast simple treats the 32 bits as being an unsigned value ?
>>>>>>>>>>FWIW:
unchecked { Int32 test = ((Int32)UInt32.MaxValue);
>gives -1
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>Yes, of course, all bits set is signed -1 and unsigned the max value
>>>>>>>>>
>>>>>>>>>When the leftmost bit is set in a signed int type, then it is considered as negative
>>>>>>>>>
>>>>>>>>>>But if the signed int is negative then there is no accurate equivalent for an unsigned int ?
>>>>>>>>>
>>>>>>>>>Viv, this isn't about maths. The crc32 is just a structure that is 32 bits wide
>>>>>>>>
>>>>>>>>That's why I said : "I'm assuming the C# cast simple treats the 32 bits as being an unsigned value"
>>>>>>>>
>>>>>>>>>If you think about it as a signed 32 bit structure and the leftmost bit is set then the number is negative
>>>>>>>>>
>>>>>>>>>If you look at it as an unsigned 32 bit structure, the same pattern is a number >= 0
>>>>>>>>
>>>>>>>>I understand that - my point was that, in normal application usage, converting a negative signed int to an unsigned int would not make sense.
>>>>>>>
>>>>>>>
>>>>>>>Well, if the cast does not work, I think I have figured out how to - pardon the syntax - and untested
>>>>>>>
>>>>>>>If the number is not negative, I see no problem in converting
>>>>>>
>>>>>>What if the unsigned positive value > Int32.MaxValue() ?
>>>>>>
>>>>>
>>>>>Not possible - it is a 32 bit wide structure that is interpreted as a signed 32 bit integer
>>>>
>>>>You mean it's not possible for unsigned positives to be larger than an Int32 ? But
>>>>Int32.MaxValue = 2147483647
>>>>UInt32.MavValue = 4294967295
>>>
>>>
>>>I gave a wrong answer
>>>
>>>
>>>All the manipulation (and, xor, shift) is done on a 32 bit structure. So you only have 32 bits
>>>
>>>In the signed world, the leftmost bit is the sign bit which leaves 31 bits for the 'number'
>>>In the unsigned world, there is no sign bit, it's >= 0 , which leaves 32 bits for the number
>>>
>>>
>>>So whatever manipulation you do, the data stays in 32 bits
>>>
>>>In the end, you 'express' what is in the data. You speak signed language or unsigned language, but the internal representation is the same
>>
>>I think that's exactly what I've just said on the other thread branch :-}
>>But that fact remains that there can be no valid cast to Int32 for UInt32 values > Int32.MaxValue.....
>
>
>
>>But that fact remains that there can be no valid cast to Int32 for UInt32 values > Int32.MaxValue.....
>
>
>
>Beg to differ
>( just set up a vm with vs2010)
>
>look at case 2
>
>  static void Main(string[] args)
>        {
>
>            Int32 i32 = -1 ;
>            UInt32 u32 = UInt32.MaxValue;
>            Int32 iMinValue = Int32.MinValue;
>
>            UInt32 castToUInt32 = (UInt32)i32;
>
>            Int32 castToInt32 = (Int32)u32;
>          
>
>            // (1)
>            Console.WriteLine("Signed to Unsigned: {0} to {1}", i32, castToUInt32); // -1 to  4294967295
>           
>            //(2) 
>            Console.WriteLine("Unsigned to signed: {0} to {1}", u32, castToInt32); // 4294967295 to -1
>
>            //(3) 
>           Console.WriteLine("UInt32.MinValue to Uint32: {0} to {1}", iMinValue, (UInt32)iMinValue); // -2147483648 to 2147483648
>            // ie 0x80000000 signed: two's complement =  0x7fffffff + 1
>            // 0x8000000 unsigned = 2147483648
>            Console.ReadLine();
>
>
>        }
>
But (as ages ago) I pointed out that you *can* cast using C# but it just gives you the alternate representation of the 32 bits.
But in real world -1 does not equal 4294967295 :-}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform