Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • BitConverter.ToString()

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 365
    Comment on it

    In C#, many times we need to convert a byte array to string format. In this blog, we are going to illustrate how to convert the byte array to its corresponding string format by using BitConverter.ToString() method.

     

    Syntax:

    public static string ToString(byte[ ] value)

     

    It accepts one parameter "value" which specifies an array bytes.

    It returns a corresponding string of specified array bytes.

     

    Exceptions:

    This method throws only "ArgumentNullException" if value parameter is null.

     

    It can be used in the following ways:

     

    1. ToString(Byte[ ]) ------> This method is used to converts the numerical value of each element of defined array byte to its corresponding string format.

    2. ToString(Byte[ ], Int32) ----> This method is used to converts the numerical value of each element of defined subarray byte to its corresponding string format. It takes the start index value of a subarray.

    3. ToString(Byte[ ], Int32, Int32) ------> This method is used to converts the numerical value of each element of defined subarray byte to its corresponding string format. It takes the start index value of a subarray and the length of the subarray. If we specify the length value “n” it will go upto the (n-1) subarray.

     

    To convert byte array to string using BitConverter.ToString( Byte[ ] ) method, see the below code:

    namespace ByteToStringConApp
     {
        class Program
         {
            static void Main(string[] args)
            {
    
                byte[] arrayBytes = { 100, 222, 194, 03 };
                string str = BitConverter.ToString(arrayBytes );   
                Console.WriteLine("String: " + str);
                Console.ReadKey();
             }
         }
     }
    
    
    

     

    Output:

    String : 64-DE-C2-03

     

    To convert byte array to string using BitConverter.ToString(Byte[ ], Int32) method, see the below code:

    namespace ByteToStringConApp
     {
        class Program
         {
            static void Main(string[] args)
            {
    
                byte[] arrayBytes = { 100, 222, 194, 03 };
                string str = BitConverter.ToString(arrayBytes,1 );   
                Console.WriteLine("String: " + str);
                Console.ReadKey();
             }
         }
     }

     

    Output:

    String : DE-C2-03

     

    To convert byte array to string using BitConverter.ToString(Byte[ ], Int32, Int32) method, see the below code:

    namespace ByteToStringConApp
     {
        class Program
         {
            static void Main(string[] args)
            {
    
                byte[] arrayBytes = { 100, 222, 194, 03 };
                string str = BitConverter.ToString(arrayBytes,0,3 );   
                Console.WriteLine("String: " + str);
                Console.ReadKey();
             }
         }
     }
    

     

    Output:

    String : 64-DE-C2

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: