C# Convert ToUInt32(String, Int32)

Description

Convert ToUInt32(String, Int32) converts the string representation of a number in a specified base to an equivalent 32-bit unsigned integer.

Syntax

Convert.ToUInt32(String, Int32) has the following syntax.


[CLSCompliantAttribute(false)]//  w w  w.  ja  v a 2 s  .c  o  m
public static uint ToUInt32(
  string value,
  int fromBase
)

Parameters

Convert.ToUInt32(String, Int32) has the following parameters.

  • value - A string that contains the number to convert.
  • fromBase - The base of the number in value, which must be 2, 8, 10, or 16.

Returns

Convert.ToUInt32(String, Int32) method returns A 32-bit unsigned integer that is equivalent to the number in value, or 0 (zero) if value is null.

Example

The following example attempts to interpret each element in an array of numeric strings as a hexadecimal value and to convert it to an unsigned integer.


using System;// www  .  ja v a  2  s . c  om

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", 
                              "-13", "9AC61", "GAD", "FFFFFFFFFF" };

      foreach (string hexString in hexStrings)
      {
         Console.Write("{0,-12}  -->  ", hexString);
         try {
            uint number = Convert.ToUInt32(hexString, 16);
            Console.WriteLine("{0,18:N0}", number);
         }
         catch (FormatException) {
            Console.WriteLine("{0,18}", "Bad Format");
         }   
         catch (OverflowException)
         {
            Console.WriteLine("{0,18}", "Numeric Overflow");
         }   
         catch (ArgumentException) {
            Console.WriteLine("{0,18}", "Invalid in Base 16");
         }
      }                                            
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version