Coding Villa

Convert String into 8 bit signed integer in C#

Author : Hassan Shafiq

Share with Friends
Views : 611
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn that how we can Convert String into 8 bit signed integer using sbyte class in c#.


Advertisements


 

 

C# SByte class

 

The basic function of SByte class is to convert string data type into integer data type.It represents 8 bits or 1 byte and it stores integers between -128 and 127. It is rarely used as compared to other types. We can declare and initialize Sbyte variable like that

 

sbyte var1 = 127;

 

SByte.Parse Method

 

SByte.Parse method is used to convert the string representation of a number to its 8-bit signed integer equivalent. We will use this method to convert string into its 8-bit signed integer in the code given below. If the string type value will be smaller/larger than its defined range or will contain special characters then exception will be thrown.

 

Code

 

Using System;

 

class Program

    {

static void Main(string[] args)

        {

string[] Values = { "-15", "+100", "243", "-128", "55", "(23)" };

 

foreach(string value in Values)

            {

try

                {

Console.WriteLine("Converted '{0}' to the SByte value {1}.",

value, SByte.Parse(value));

                }

catch (FormatException)

                {

Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",

value);

                }

catch (OverflowException)

                {

Console.WriteLine("'{0}' is out of range of the SByte type.",

value);

                }

            }

Console.ReadLine();

        }

    }

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook