C# Int64, Long, automatic conversion

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

C# Int64, Long, automatic conversion

 
0
  #1
May 4th, 2005
Since nobody answered the thread on Binary File IO, I took a different approach.

Now I'm having another problem.

As I process a very large (>2GB) binary file, I store the byte position at certain points. Then, I reposition the file as needed.

I store the byte positions in a variable declared:

System.Int64 curr_pos;

However, the program fails when I position the file:

infile.Position = curr_pos;

I debug, and notice that this fails when the value of curr_pos gets over 2,147,483,647, which is the limit of a 32-bit integer. In fact, the value of curr_pos becomes negative, and that's the error I'm getting, that the value must be non-negative.

Why, since I explicitly declare the variable as 64-bit, does it fail when it reaches the 32-bit limit?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: C# Int64, Long, automatic conversion

 
0
  #2
May 5th, 2005
...and the answer to this is that the curr_pos was calculated, and one of the terms in the calculation was an Int32, casting the entire result as an Int32.

Each term has to be cast to Int64 to maintain the integral type of the result.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: C# Int64, Long, automatic conversion

 
0
  #3
May 5th, 2005
Originally Posted by tgreer
...and the answer to this is that the curr_pos was calculated, and one of the terms in the calculation was an Int32, casting the entire result as an Int32.

Each term has to be cast to Int64 to maintain the integral type of the result.
This is the opposite to almost all (typed and psuedo typed) languages, and probably not exactly what is occuring. The type of an equation usually becomes the type of the largest type used in the equation.

So ...
  1. long a = 30000L;
  2. short b = a * 3;

Will not compile because (a * 3) is of type long.. and you cannot implicitly cast down (in most languages). In C/C++, writing code like this will compile... however these langauges presume that you know what you are doing.

So what happens is the equation is cast to the larger type, but then when it comes to assignment you will suffer data loss (the answer to the above equation is 24464).

Likewise a simple way of forcing cast of an equation to double is to multiply by 1.0.

i.e. 3*1.0/4 = 0.75, but 3/4 = 0.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 16595 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC