| | |
C# Int64, Long, automatic conversion
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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?
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?
•
•
•
•
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.
So ...
C# Syntax (Toggle Plain Text)
long a = 30000L; 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.
![]() |
Similar Threads
- Ascii to Long Double conversion (C++)
- Need help friends comp is full of junk (Viruses, Spyware and other Nasties)
- How to write FNVAL functions (Java)
Other Threads in the C# Forum
- Previous Thread: How can i get i get the button.Enabled=true; work when...
- Next Thread: C# XML Comment Problems
Views: 16595 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gdi+ http image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






