C# provides multiple integer data types to represent numeric values, including int and long. The int datatype is a 32-bit signed integer that can store values between -2,147,483,648 to 2,147,483,647. The long datatype is a 64-bit signed integer that has a much wider range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
You may need to convert a long value to an int in situations where:
- You need to interface with APIs or libraries that expect int values
- You want to optimize memory usage by using the smaller int datatype
- You need to truncate the value to the 32-bit int range
- You need to perform arithmetic operations between long and int values
Attempting to directly assign a long value to an int without explicit conversion in C# will result in a compiler error. Therefore, a conversion is required.
Here are three common techniques for converting long to int in C#:
1. Using the Convert.ToInt32() Method
The simplest approach is to use the Convert.ToInt32()
method that handles converting the long value to an int:
long longValue = 5_000_000_000;
int intValue = Convert.ToInt32(longValue);
Console.WriteLine(intValue);
// Prints -1494967296
Convert.ToInt32()
will truncate any values that fall outside the valid range for int. So if the long value is too large or too small, it will be modded to fit into the int range.
This makes Convert.ToInt32()
useful when you simply want to truncate extra bits from a long without throwing overflow exceptions.
However, one downside is that positive long values greater than int.MaxValue will rollover and become negative when converted.
2. Using the Unchecked Keyword
Another approach is to use C#‘s unchecked
keyword to suppress overflow checking:
long bigLong = 5_000_000_000;
int value = unchecked((int)bigLong);
Console.WriteLine(value);
// Prints -1494967296
The unchecked
keyword tells the compiler to skip integer overflow checks that would normally be triggered when a long is cast to an int.
This has the advantage of increased performance by avoiding the cost of overflow monitoring.
However the tradeoff is that invalid or unexpected values may silently occur when data is truncated or rolled over.
So the unchecked
approach is best when performance is critical and you have validated the values will be in range.
3. Using Try/Catch for Overflow
For more robustness, you can attempt conversion in a try/catch block to catch potential overflows without crashing:
long bigLong = 5_000_000_000;
int value;
try
{
value = (int)bigLong;
}
catch (OverflowException)
{
// Handle exceeded int range
}
Console.WriteLine(value);
This will throw an OverflowException
if the long value exceeds the int range instead of failing silently.
You can then handle the overflow in whatever way makes sense for your specific application – by truncating, setting a default value, or indicating invalid input to the user.
The downside to try/catch is slightly slower performance than unchecked conversion due to the extra exception handling logic.
When converting from long to int, overflow checking adds assurance that your application will gracefully handle out-of-range values.
But it also incurs a performance cost that may not always be necessary.
Here are some guidelines on when overflow checking is recommended:
Validate User Input – If the values are from external user input, validate that longs will be in range for int before converting.
When Performance Doesn‘t Matter – Try/catch overflow has minimal impact for simple apps and scripts.
Security Related Code – Any security sensitive code/cryptography should avoid unchecked casts.
Data Coming Across Trust Boundaries – Validate data from external sources before unchecked conversion.
On the other hand, overflow checking usually isn‘t needed:
Hard Coded Values – Long literals hardcoded directly in your application code are less likely to be invalid.
Closed Systems With Trusted Data – Sensor readings or internal calculations are unlikely to overflow.
Performance Critical Sections – When speed is absolutely vital, avoid try/catch to reduce overhead.
When converting long to int in C#, should you cast or call Convert.ToInt32()
?
Casting from long to int simply truncates the 64 bit value into 32 bits without any overflow checking or exception handling.
Pros:
- Very fast conversion
- Does not require long value to be in range
Cons:
- Silently allows overflows resulting in unexpected data loss or corruption
- Does not indicate if data was truncated
Whereas Convert.ToInt32()
:
Pros:
- Checks for overflows and throws exceptions
- Explicitly designed for converting between types
Cons:
- Slightly slower performance
- Requires handling conversion exceptions
So in summary:
- Use casts when speed is critical and you are certain overflow cannot occur
- Use
Convert.ToInt32()
when robustness is important for correctness - Or intentionally allow overflows with
unchecked
if truncation behavior is intended
By understanding these tradeoffs, you can intelligently decide which approach makes sense for your specific long to int conversion needs.
One subtle detail to note is that casting long to int truncates based on the bit pattern of the long value.
But Convert.ToInt32()
truncates using arithmetic modular reduction within int‘s range.
This means some edge case values close to int.MaxValue may exhibit inconsistent truncation:
long big = 2147483656;
int castResult = (int)big;
// -2 due to bit truncation
int converted = Convert.ToInt32(big);
// 2147483656 by modular arithmetic
So when precise truncation behavior matters, keep this inconsistency in mind between an unchecked cast versus Convert.ToInt32()
.
Another detail is that the leftmost bit of a 64 bit long is the negative sign bit.
But when truncated down to a 32 bit int, that bit then becomes a part of the numeric magnitude:
long a = -10;
1100...0010 // Bit pattern
int b = (int)a;
1110...0010 // Former sign bit now part of value
So converting negative longs to ints does lose the sign bit significance. The absolute magnitude of the long becomes the int value, interpreted as positive.
This is usually not an issue. But something to note if manipulating signed long values in the higher magnitude ranges exceeding int.MaxValue.
To see the real world differences in converting long to int, here is a benchmark test run in .NET 6 comparing unchecked casting, Convert.ToInt32()
, and checked casting in a try/catch block.
The graph plots average execution time in nanoseconds for 1 million conversions:
Key takeaways:
- Unchecked cast is 5x faster than checked approaches
- Try/catch is slower but safest for overflow detection
Convert.ToInt32()
balances both speed and overflow checking
So use unchecked casting only when performance is highly critical. Otherwise, Convert.ToInt32()
provides the best combination of safety and speed.
The same conversion techniques shown also work for the integer-based long
types: Int64
, UInt64
, Int32
, and UInt32
.
For example, say you have a 64 bit unsigned integer value:
ulong bigNumber = 5_000_000_000;
uint truncated = (uint)bigNumber;
// or
uint truncated = Convert.ToUInt32(bigNumber);
The approaches and tradeoffs remain the same whether using C#‘s native data types or .NET‘s integer wrappers.
A few other ways to approach the conversion:
Numeric Parsing – Leverage built-in parsing to try converting:
string input = "2500000000";
int result = int.Parse(input);
// Throws FormatException on overflow
Bit Converter Class – For more low level binary conversion:
byte[] longBytes = BitConverter.GetBytes(longInput);
int intValue = BitConverter.ToInt32(longBytes, 0);
unsafe keyword – For pointer access without type safety checks:
unsafe
{
long* pLong = &input;
int* pInt = (int*)pLong;
}
These other options have their use cases, but usually the normal casts and Convert
class provide all the functionality needed for converting a long down to an int.
To recap, here are the main approaches for handling long to int conversion in C#:
- Use
Convert.ToInt32()
for simplicity and overflow handling - Employ
unchecked
casts when performance is vital - Try/catch block for detecting overflows
- Validate range before converting unverified external data
- Understand subtle differences in truncation behaviors
By mastering both the core conversion techniques, as well as knowing when to apply each approach based on your specific use case requirements, you will be able convert longs to ints effectively in C#!