Search code examples
c#arraysbytecodeconcatenationbitconverter

c# concatenate byte[] and get string result


I came across one requirement, in which i have system.byte[] value coming from database. Now i need to get string value from that bye[] values. I am iterating datatable values using datarow. There are so many columns coming with system.byte[] value. How can i check system.byte[] value and convert it into string show as a result?


Solution

  • You are asking two questions here : "How to concate" and "How to convert".

    using System;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Program
    {
        public static void Main()
        {
            byte[] bytes1 = { 97, 98, 99, 100 };
            byte[] bytes2 = { 49, 50, 51, 52 };
    
            // concat
            byte[] bytes = bytes1.Concat(bytes2).ToArray();
            // convert
            string bytesAsString = Encoding.UTF8.GetString(bytes);
    
            Console.WriteLine(bytesAsString);
    
        }
    }
    

    Demo DotNetFiddle