.Net C# String encoding

A look at the various encoding systems available in .Net.

using System;
using System.Collections.Generic;
using System.Text;

namespace CharpStringDemo
{
    internal class CharpStringExample
    {
        private static IList<byte[]> _bytes;

        private static void Main()
        {
            _bytes = new List<byte[]>();
         
            const string input = "Csharp";   

            var result1 = Encoding.UTF7.GetBytes(input);
            _bytes.Add(result1);
            
            var result2 = Encoding.UTF8.GetBytes(input);
            _bytes.Add(result2);
            
            var result3 = Encoding.Unicode.GetBytes(input);
            _bytes.Add(result3);
            
            var result4 = Encoding.UTF32.GetBytes(input);
            _bytes.Add(result4);
            
            var result5 = Encoding.Default.GetBytes(input);
            _bytes.Add(result5);
            
            var result6 = Encoding.BigEndianUnicode.GetBytes(input);
            _bytes.Add(result6);
            
            var result7 = Encoding.ASCII.GetBytes(input);
            _bytes.Add(result7);

            foreach (var bytes in _bytes)
            {
                foreach (var code in bytes)
                {
                    Console.Write("{0} ", code);
                }

                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

Returns:

67 115 104 97 114 112
67 115 104 97 114 112
67 0 115 0 104 0 97 0 114 0 112 0
67 0 0 0 115 0 0 0 104 0 0 0 97 0 0 0 114 0 0 0 112 0 0 0
67 115 104 97 114 112
0 67 0 115 0 104 0 97 0 114 0 112
67 115 104 97 114 112

Leave a Reply

Your email address will not be published. Required fields are marked *