Bug 981 - sbyte.Parse, short.Parse raise OverflowException when parsing hexadecimal negative numbers
Summary: sbyte.Parse, short.Parse raise OverflowException when parsing hexadecimal neg...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 2.10.x
Hardware: PC Linux
: --- critical
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-09-22 04:01 UTC by Angel Cervantes
Modified: 2011-11-04 09:15 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Angel Cervantes 2011-09-22 04:01:19 UTC
The implementation of methods:
sbyte.Parse( string s, System.Globalization.NumberStyles style );
short.Parse( string s, System.Globalization.NumberStyles style );
do not work when the string is the hexadecimal representation of a negative number.

The following code in the csharp console reproduces the error:
csharp> using System.Globalization;
csharp> sbyte.Parse( "FF", NumberStyles.HexNumber );
csharp> short.Parse( "FFFF", NumberStyles.HexNumber );

Actual results:
An OverflowException is raised

Expected results:
The same methods using the .NET framework return -1

Mono version: 2.10.3 (tarball)
Comment 1 Angel Cervantes 2011-09-22 04:04:52 UTC
A workaround for this issue is using the unsigned type Parse method, and then casting to the signed number.

Example:
var n = (sbyte)byte.Parse( "FF", NumberStyles.HexNumber );
Comment 2 Marek Safar 2011-11-04 09:15:55 UTC
Fixed in master