1

I want to cast an Int32 value to Int16 value. Data lost when casting is not a problem to me. But a System.OverflowException says the Int16 type value is too large or too small.

Dim num As Int32 = &HFFFFFFF
Dim num2 As Int16 = Convert.ToInt16(num)
Debug.WriteLine(num.ToString("X4"))
Debug.WriteLine(num2.ToString("X4"))

If I want to cast an Int32 &HFFFFFFF to &HFFFF so what should I do.

Any help would be appreciated.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Yen NQ
  • 749
  • 7
  • 12
  • 2
    I've fixed your tag - `OverflowException` and `StackOverflowException` are *very* different. – Jon Skeet Feb 24 '15 at 07:21
  • You're getting the overflowException error because your int32 value is greater than Int16.MaxValue or less than Int16.MinValue. – Amen Jlili Feb 24 '15 at 07:35
  • IMO If I have to check limitation or using try..catch before casting, it would be not very nice since the code will longer. – Yen NQ Feb 24 '15 at 07:42
  • `Dim num2 As Int16 = CShort(IIf((num And &HFFFF) < 32768, (num And &HFFFF), (num And &HFFFF) - 65536))` – The Blue Dog Feb 24 '15 at 08:14

2 Answers2

1

I think, as I said in my comment, that your casting is invalid because int16 has maxValue and MinValue which your int32 obviously does not sit in between.

Try the following to see your error more clearly:

 Debug.WriteLine(Int16.MaxValue.ToString)
 Debug.WriteLine(Int16.MinValue.ToString)
 Debug.WriteLine(num.ToString)

enter image description here

Your best workaround is to trim the last 4 F off your int32 whenever converting, if you still insist on doing it:

 Sub Main()
        Dim num As Int32 = &HFFFFFFF
        Dim num2 As Int16 = Convert.ToInt16(num.ToString("X8").Substring(num.ToString("X8").Length - 4, 4), 16)
        Debug.WriteLine(num.ToString("X4"))
        Debug.WriteLine(num2.ToString("X4"))
        Console.ReadLine()
    End Sub
Amen Jlili
  • 1,884
  • 4
  • 28
  • 51
  • It produces `FFFFFFF` and `68DB` since `ToString` prints decimal string by default and `Remove` is not quite reasonable here. I correct the conversion to `Dim num2 As Int16 = Convert.ToInt16(num.ToString("X8").Substring(num.ToString("X8").Length - 4, 4), 16)` and it works. Thanks! – Yen NQ Mar 02 '15 at 01:58
  • I will accept your answer if you can correct it to produce the right output. 1+ for the information – Yen NQ Mar 02 '15 at 02:01
  • @QuangYen: There you go. – Amen Jlili Mar 02 '15 at 14:37
1

If you can ignore OverflowException while casting and arithmetic operations (including division by zero) across entire your project, I'd suggest you set the compiler option /removeintchecks.

To set /removeintchecks in the Visual Studio integrated development environment

  1. Have a project selected in Solution Explorer. On the Project menu, click Properties. For more information, see Introduction to the Project Designer.
  2. Click the Compile tab.
  3. Click the Advanced button.
  4. Modify the value of the Remove integer overflow checks box.

And, Convert.ToInt16() still generates the OverflowException regardless of that option. To benefit from that option, you can use CType or CShort instead.

Dim num As Int32 = &HFFFFFFF
Dim num2 As Int16 = CType(num, Int16)
Debug.WriteLine(num.ToString("X4"))
Debug.WriteLine(num2.ToString("X4"))
Ripple
  • 1,257
  • 1
  • 9
  • 15