Data conversion in DC, unsigned long to long

I have an unsigned long integer I wish to interpret as a long integer. That means I want to change the representation without changing the bit pattern. Thus if bit 31 is set I want the number to be interpreted as a negative number, not an integer larger than 2^31-1. If I use type casting any number with bit 31 set is interpreted as 2^31-1. Throwing away half of my number space.

All I want is a long variable C_sav to interpret the data in unsigned long variable data.

I tried using pointers but type casting was performed anyway.

Here’s my code:

unsigned long data;
long *d_ptr;

d_ptr = &data;

C_sav = *d_ptr;

I get a warning message incompatible types for the statement d_ptr = &data; and the statement C_sav = *d_ptr; still performs type casting just as if I had written C_sav = data (or C_sav = (long) data;

Why can’t I just interpret the bits in data as a long without the data conversion?

Thanks,

Jim

The stroll function is returning negative numbers as 0X7FFFFFF instead of as a negative integer. That means it’s looking at the number as an unsigned integer and type casting it to long. I haven’t been able to figure out why this is happening.

Does anyone have nay experience with the strtol function with negative numbers?

Thanks,

Jim

I am using the DC 7.62 compiler with a RCM3400. I have been having a problem with sting to long conversions so I wrote a small piece of code to test the strtol function.

main()
{
long num1;

brdinit();

num1 = -2;

num1 = strtol(“FFFFFFFF”, NULL,16);

printf("%s, %ld
", "num1= ", num1);

}

If I inspect num1 just after I define it it is 0xFFFFFFFE as it should be. However, when I convert the sting to long I get 0x7FFFFFFF. When I print after the conversion I get the value 2157483647, and the debugger shows 2147483647 or 0x7FFFFFFF.

If I convert the string 0x7FFFFFFE the debugger shows 2147483646 or 0x7FFFFFFE, The printed value is 2147483646.

The strtol function does not deal properly with negative numbers.

Is this a bug in the code or is there something I don’t understand?

Jim