Convert String to Byte Array and Byte Array to String in C#

Description

In this article, I am going to write C# code to convert Byte Array to String and String to Byte Array by using Encoding.UTF8 and Encoding.ASCII.

Summary

Convert String to Byte Array in C# using Encoding.UTF8

You can convert String value into byte [] easily by using Encoding.UTF8.

static void Main(string[] args)
    {
        string value = "String value to convert byte Array";

        byte[] byteData = ConvertStringByteArrayUsingEncodingUTF8(value);

        Console.WriteLine(byteData.Length);
    }

    private static byte[] ConvertStringByteArrayUsingEncodingUTF8(string strData)
    {
        byte[] byteData = Encoding.UTF8.GetBytes(strData);

        return byteData;
    }

Convert Byte Array to String in C# using Encoding.UTF8

You can convert Byte Array into String value easily by using Encoding.UTF8.

static void Main(string[] args)
    {
        string value = "String value to convert byte Array";

        byte[] byteData = ConvertStringByteArrayUsingEncodingUTF8(value);

        string value2 = ConvertByteArrayToStringUsingEncodingUTF8(byteData);

        Console.WriteLine(value2);
    }

    private static byte[] ConvertStringByteArrayUsingEncodingUTF8(string strData)
    {
        byte[] byteData = Encoding.UTF8.GetBytes(strData);

        return byteData;
    }

    private static string ConvertByteArrayToStringUsingEncodingUTF8(byte[] byteData)
    {
        string strData = Encoding.UTF8.GetString(byteData);

        return strData;
    }

Convert String to Byte Array in C# using Encoding.ASCII

You can convert String to Byte Array easily by using Encoding.ASCII in C#.

Note: Please note that this convert operation may cause some data loss if you are using some characters that are not in the ASCII character set.

static void Main(string[] args)
    {
        string value = "String value to convert byte Array";

        byte[] byteData = ConvertStringByteArrayUsingASCII(value);

        Console.WriteLine(byteData.Length);
    }

    private static byte[] ConvertStringByteArrayUsingASCII(string strData)
    {
        byte[] byteData = Encoding.ASCII.GetBytes(strData);

        return byteData;
    }

Convert Byte Array to String in C# using ASCIIEncoding.ASCII

You can convert Byte Array into string value easily by using ASCIIEncoding.ASCII in C#.

static void Main(string[] args)
    {
        string value = "String value to convert byte Array";

        byte[] byteData = ConvertStringByteArrayUsingASCII(value);

        string value2 = ConvertByteArrayToStringUsingConvert(byteData);

        Console.WriteLine(value2);
    }

    private static byte[] ConvertStringByteArrayUsingASCII(string strData)
    {
        byte[] byteData = Encoding.ASCII.GetBytes(strData);

        return byteData;
    }

    private static string ConvertByteArrayToStringUsingConvert(byte[] byteData)
    {
        string strData = ASCIIEncoding.ASCII.GetString(byteData);

        return strData;
    }

Thanks,
Morgan
Software Developer


Advertisement

Leave a Comment