CopyPastehas never been so tasty!

Convert String into 8 bit signed integer in C#

by anonymous

  • 0
  • 0
  • 0
111 views

Convert String into 8 bit signed integer in C#

C# SByte classThe 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 MethodSByte.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();

        }

    }

Add A Comment: