Export (0) Print
Expand All

Convert.ToUInt32 Method (Object, IFormatProvider)

Converts the value of the specified object to a 32-bit unsigned integer, using the specified culture-specific formatting information.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function ToUInt32 ( _
	value As Object, _
	provider As IFormatProvider _
) As UInteger
'Usage
Dim value As Object 
Dim provider As IFormatProvider 
Dim returnValue As UInteger 

returnValue = Convert.ToUInt32(value, _
	provider)

Parameters

value
Type: System.Object

An object that implements the IConvertible interface.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.UInt32
A 32-bit unsigned integer that is equivalent to value, or zero if value is Nothing.

ExceptionCondition
FormatException

value is not in an appropriate format.

InvalidCastException

value does not implement the IConvertible interface.

-or-

The conversion is not supported.

OverflowException

value represents a number that is less than UInt32.MinValue or greater than UInt32.MaxValue.

The return value is the result of invoking the IConvertible.ToUInt32 method of the underlying type of value.

provider enables the user to specify culture-specific conversion information about the contents of value. For example, if value is a String that represents a number, provider could supply culture-specific information about the notation used to represent that number.

The base types ignore provider; however, the parameter may be used if value is a user-defined type that implements the IConvertible interface.

The following example defines a HexString class that implements the IConvertible interface and that is designed to hold the string representation of both 32-bit signed and 32-bit unsigned values. The class includes a Sign property that indicates the sign of its hexadecimal value.

Imports System.Globalization
Imports System.Text.RegularExpressions

Public Enum SignBit As Integer
   Positive = 1
   Zero = 0
   Negative = -1
End Enum 

Public Structure HexString : Implements IConvertible
   Private signBit As SignBit
   Private hexString As String 

   Public Property Sign As SignBit
      Set
         signBit = value
      End Set 
      Get 
         Return signBit
      End Get 
   End Property 

   Public Property Value As String 
      Set 
         If value.Trim().Length > 8 Then 
            Throw New ArgumentException("The string representation of a 32-bit integer cannot have more than 8 characters.")
         Else If Not Regex.IsMatch(value, "([0-9,A-F]){1,8}", RegexOptions.IgnoreCase) Then 
            Throw New ArgumentException("The hexadecimal representation of a 32-bit integer contains invalid characters.")             
         Else
            hexString = value
         End If    
      End Set 
      Get 
         Return hexString
      End Get 
   End Property 

   ' IConvertible implementations. 
   Public Function GetTypeCode() As TypeCode _
                   Implements IConvertible.GetTypeCode
      Return TypeCode.Object
   End Function 

   Public Function ToBoolean(provider As IFormatProvider) As Boolean _
                   Implements IConvertible.ToBoolean
      Return signBit <> SignBit.Zero
   End Function  

   Public Function ToByte(provider As IFormatProvider) As Byte _
                   Implements IConvertible.ToByte
      If signBit = SignBit.Negative Then 
         Throw New OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToInt32(hexString, 16))) 
      Else 
         Try 
            Return Byte.Parse(hexString, NumberStyles.HexNumber)
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToUInt32(hexString, 16)), e)
         End Try    
      End If        
   End Function 

   Public Function ToChar(provider As IFormatProvider) As Char _
                   Implements IConvertible.ToChar
      If signBit = SignBit.Negative Then  
         Throw New OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToInt32(hexString, 16)))
      End If 

      Try 
         Dim codePoint As UInt16 = UInt16.Parse(Me.hexString, NumberStyles.HexNumber)
         Return Convert.ToChar(codePoint)
      Catch e As OverflowException
         Throw New OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToUInt32(hexString, 16)))
      End Try       
   End Function  

   Public Function ToDateTime(provider As IFormatProvider) As Date _
                   Implements IConvertible.ToDateTime
      Throw New InvalidCastException("Hexadecimal to DateTime conversion is not supported.")
   End Function 

   Public Function ToDecimal(provider As IFormatProvider) As Decimal _
                   Implements IConvertible.ToDecimal
      If signBit = SignBit.Negative Then 
         Dim hexValue As Integer = Int32.Parse(hexString, NumberStyles.HexNumber)
         Return Convert.ToDecimal(hexValue)
      Else 
         Dim hexValue As UInteger = UInt32.Parse(hexString, NumberStyles.HexNumber)
         Return Convert.ToDecimal(hexValue)
      End If 
   End Function 

   Public Function ToDouble(provider As IFormatProvider) As Double _
                   Implements IConvertible.ToDouble
      If signBit = SignBit.Negative Then 
         Return Convert.ToDouble(Int32.Parse(hexString, NumberStyles.HexNumber))
      Else 
         Return Convert.ToDouble(UInt32.Parse(hexString, NumberStyles.HexNumber))
      End If    
   End Function    

   Public Function ToInt16(provider As IFormatProvider) As Int16 _
                   Implements IConvertible.ToInt16
      If signBit = SignBit.Negative Then 
         Try 
            Return Convert.ToInt16(Int32.Parse(hexString, NumberStyles.HexNumber))
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToInt32(hexString, 16)), e)
         End Try 
      Else 
         Try 
            Return Convert.ToInt16(UInt32.Parse(hexString, NumberStyles.HexNumber))
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToUInt32(hexString, 16)), e)
         End Try 
      End If    
   End Function 

   Public Function ToInt32(provider As IFormatProvider) As Int32 _
                   Implements IConvertible.ToInt32
      If signBit = SignBit.Negative Then 
         Return Int32.Parse(hexString, NumberStyles.HexNumber)
      Else 
         Try 
            Return Convert.ToInt32(UInt32.Parse(hexString, NumberStyles.HexNumber))
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is out of range of the Int32 type.", Convert.ToUInt32(hexString, 16)), e)
         End Try    
      End If    
   End Function 

   Public Function ToInt64(provider As IFormatProvider) As Int64 _
                   Implements IConvertible.ToInt64
      If signBit = SignBit.Negative Then 
         Return Convert.ToInt64(Int32.Parse(hexString, NumberStyles.HexNumber))
      Else 
         Return Int64.Parse(hexString, NumberStyles.HexNumber)
      End If    
   End Function 

   Public Function ToSByte(provider As IFormatProvider) As SByte _
                   Implements IConvertible.ToSByte
      If signBit = SignBit.Negative Then 
         Try 
            Return Convert.ToSByte(Int32.Parse(hexString, NumberStyles.HexNumber))
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is outside the range of the SByte type.", _
                                                      Int32.Parse(hexString, NumberStyles.HexNumber), e))
         End Try 
      Else 
         Try 
            Return Convert.ToSByte(UInt32.Parse(hexString, NumberStyles.HexNumber))
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is outside the range of the SByte type.", _
                                                    UInt32.Parse(hexString, NumberStyles.HexNumber)), e)
         End Try    
      End If 
   End Function 

   Public Function ToSingle(provider As IFormatProvider) As Single _
                   Implements IConvertible.ToSingle
      If signBit = SignBit.Negative Then 
         Return Convert.ToSingle(Int32.Parse(hexString, NumberStyles.HexNumber))
      Else 
         Return Convert.ToSingle(UInt32.Parse(hexString, NumberStyles.HexNumber))
      End If    
   End Function 

   Public Overloads Function ToString(provider As IFormatProvider) As String _
                   Implements IConvertible.ToString
      Return "0x" & Me.hexString
   End Function 

   Public Function ToType(conversionType As Type, provider As IFormatProvider) As Object _
                   Implements IConvertible.ToType
      Select Case Type.GetTypeCode(conversionType)
         Case TypeCode.Boolean  
            Return Me.ToBoolean(Nothing)
         Case TypeCode.Byte
            Return Me.ToByte(Nothing)
         Case TypeCode.Char 
            Return Me.ToChar(Nothing)
         Case TypeCode.DateTime
            Return Me.ToDateTime(Nothing)
         Case TypeCode.Decimal
            Return Me.ToDecimal(Nothing)
         Case TypeCode.Double 
            Return Me.ToDouble(Nothing)
         Case TypeCode.Int16
            Return Me.ToInt16(Nothing)
         Case TypeCode.Int32
            Return Me.ToInt32(Nothing)
         Case TypeCode.Int64
            Return Me.ToInt64(Nothing)
         Case TypeCode.Object
            If GetType(HexString).Equals(conversionType) Then 
               Return Me 
            Else 
               Throw New InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name))
            End If  
         Case TypeCode.SByte
            Return Me.ToSByte(Nothing)
         Case TypeCode.Single
            Return Me.ToSingle(Nothing)
         Case TypeCode.String 
            Return Me.ToString(Nothing)
         Case TypeCode.UInt16
            Return Me.ToUInt16(Nothing)
         Case TypeCode.UInt32
            Return Me.ToUInt32(Nothing)
         Case TypeCode.UInt64
            Return Me.ToUInt64(Nothing)   
         Case Else 
            Throw New InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name))   
      End Select 
   End Function 

   Public Function ToUInt16(provider As IFormatProvider) As UInt16 _
                   Implements IConvertible.ToUInt16
      If signBit = SignBit.Negative Then 
         Throw New OverflowException(String.Format("{0} is outside the range of the UInt16 type.", _
                                                   Int32.Parse(hexString, NumberStyles.HexNumber)))
      Else 
         Try 
            Return Convert.ToUInt16(UInt32.Parse(hexString, NumberStyles.HexNumber))
         Catch e As OverflowException
            Throw New OverflowException(String.Format("{0} is out of range of the UInt16 type.", Convert.ToUInt32(hexString, 16)), e)
         End Try             
      End If    
   End Function 

   Public Function ToUInt32(provider As IFormatProvider) As UInt32 _
                   Implements IConvertible.ToUInt32
      If signBit = SignBit.Negative Then 
         Throw New OverflowException(String.Format("{0} is outside the range of the UInt32 type.", _
                                                   Int32.Parse(hexString, NumberStyles.HexNumber)))
      Else 
         Return Convert.ToUInt32(hexString, 16)
      End If    
   End Function 

   Public Function ToUInt64(provider As IFormatProvider) As UInt64 _
                   Implements IConvertible.ToUInt64
      If signBit = SignBit.Negative Then 
         Throw New OverflowException(String.Format("{0} is outside the range of the UInt64 type.", _
                                                   Int32.Parse(hexString, NumberStyles.HexNumber)))
      Else 
         Return Convert.ToUInt64(hexString, 16)
      End If    
   End Function 

End Structure

The following example shows that a call to the ToUInt32(Object, IFormatProvider) method that passes a HexString object as a parameter, in turn, calls the IConvertible.ToUInt32 implementation of the HexString class.

Module Example
   Public Sub Main()
      Dim positiveValue As UInt32 = 320000000
      Dim negativeValue As Int32 = -1


      Dim positiveString As New HexString()
      positiveString.Sign = CType(Math.Sign(positiveValue), SignBit)
      positiveString.Value = positiveValue.ToString("X4")

      Dim negativeString As New HexString()
      negativeString.Sign = CType(Math.Sign(negativeValue), SignBit)
      negativeString.Value = negativeValue.ToString("X4")

      Try
         Console.WriteLine("0x{0} converts to {1}.", positiveString.Value, Convert.ToUInt32(positiveString))
      Catch e As OverflowException
         Console.WriteLine("{0} is outside the range of the UInt32 type.", _
                           Int32.Parse(positiveString.Value, NumberStyles.HexNumber))
      End Try 

      Try
         Console.WriteLine("0x{0} converts to {1}.", negativeString.Value, Convert.ToUInt32(negativeString))
      Catch e As OverflowException
         Console.WriteLine("{0} is outside the range of the UInt32 type.", _
                           Int32.Parse(negativeString.Value, NumberStyles.HexNumber))
      End Try    
   End Sub 
End Module 
' The example dosplays the following output: 
'       0x1312D000 converts to 320000000. 
'       -1 is outside the range of the UInt32 type.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft