C#
  Home arrow C# arrow Page 6 - Numbers
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Numbers
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 19
    2004-06-29

    Table of Contents:
  • Numbers
  • Determining Approximate Equality Between a Fraction and Floating-Point Value
  • Converting Degrees to Radians and Converting Radians to Degrees
  • Using the Bitwise Complement Operator with Various Data Types
  • Test for an Even or Odd Value
  • Converting a Number in Another Base to Base10 and Determining Whether a String Is a Valid Number
  • Determining Whether a String Is a Valid Number
  • Rounding a Floating-Point Value and Different Rounding Algorithms Problem
  • Converting Celsius to Fahrenheit and
  • Safely Performing a Narrowing Numeric Cast
  • Discussion
  • Finding the Length of Any Three Sides of a Right Triangle and Finding the Angles of a Right Triangle Problem

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Numbers - Converting a Number in Another Base to Base10 and Determining Whether a String Is a Valid Number


    (Page 6 of 12 )

    1.7 Converting a Number in Another Base to Base10

    Problem

    You have a string containing a number in base2 (binary), base8 (octal), base10 (decimal), or base16 (hexadecimal). You need to convert this string to its equivalent integer value and display it in base10.

    Solution

    Use the overloaded static Convert.ToInt32 method on the Convert class:

    string base2 = "11";
    string base8 = "17";
    string base10 = "110";
    string base16 = "11FF";

    Console.WriteLine("Convert.ToInt32(base2, 2) = " + Convert.ToInt32(base2, 2));

    Console.WriteLine("Convert.ToInt32(base8, 8) = " + Convert.ToInt32(base8, 8));

    Console.WriteLine("Convert.ToInt32(base10, 10) = " + Convert.ToInt32(base10, 10));

    Console.WriteLine("Convert.ToInt32(base16, 16) = " + Convert.ToInt32(base16, 16));

    This code produces the following output:

    Convert.ToInt32(base2, 2) = 3
    Convert.ToInt32(base8, 8) = 15
    Convert.ToInt32(base10, 10) = 110
    Convert.ToInt32(base16, 16) = 4607

    Discussion

    The static Convert.ToInt32 method has an overload that takes a string containing a number and an integer defining the base of this number. This method then converts the numeric string into an integer and returns this number displayed as base10.

    The other static methods of the Convert class, such as ToByte, ToInt64, and ToInt16, also have this same overload, which accepts a number as a string and a base type for this number. Unfortunately, these methods convert from base2, base8, base10, and base16 only to a value of base10. They do not convert a value to any other base types.

    See Also: See the “Convert Class” and “Converting with System.Convert” topics in the MSDN documentation.

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT