In VB, the following assignments below have negative values:
Dim a As Long = &HEFCDAB89
Dim b As Long = &H98BADCFE
Assigning the same hexadecimal values in C# does not result in the same values:
long a = 0xEFCDAB89;
long b = 0x98BAD开发者_StackOverflowCFE;
How can I assign negative hexadecimal values in C# as it is in VB?
Use the L and UL literals:
long b = 0xEFCDAB89L; //signed long
long c = (long)0x98BADCFEUL; //unsigned long
I don't know of any tricks to tell the compiler that you want to specify a negative hex literal, I'm not sure that's really possible. I believe your only option is to cast the ulong
literal to a long
. It will have to be done in an unchecked context. It is still a value known at compile time so no actual casting code will be generated.
long value = unchecked((long)0xFFFFFFFFFFFFFFFE);
// value == -2L
Note that in VB, &HEFCDAB89
and &H98BADCFE
are actually Integer
literals. They just happened to be promoted to a Long
. To specify a Long
literal, you must include the suffix L
. So you assignments would have to be:
Dim a As Long = &HFFFFFFFFEFCDAB89L ' these are actually long literals now
Dim b As Long = &HFFFFFFFF98BADCFEL
精彩评论