how to get the encoding of text?

mc 4,576 Reputation points
2024-11-07T04:41:04.5733333+00:00

how to get the encoding?

if I have text like this:Καλώς ήρθατε στην επίσκεψη

how to get its encoding?

I know it is in windows-1253 but if there is textBox and user input some text how to get the encoding?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,917 questions
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 3,201 Reputation points
    2024-11-07T06:12:17.76+00:00

    if there is textBox and user input some text how to get the encoding?

    If you are talking about the TextBox of .NET application such as Windows Forms there is no way to get encoding.

    Encoding does not matter in the .NET application as string will be treated in unicode encoding.

    You will probably have to consider encoding only when you get string form byte array (e.g., text file) or convert string to byte array. Please see the following sample which explains the above.

    using System;
    using System.Data.SqlClient;
    using System.Text;
    
    namespace ConsoleApp9
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                string textBoxString = "Καλώς ήρθατε στην επίσκεψη";
    
                // Create two different encodings.            
                Encoding unicode = Encoding.Unicode;
                Encoding win1253 = Encoding.GetEncoding("windows-1253");
    
                // Convert the string into a byte[].
                byte[] unicodeBytes = unicode.GetBytes(textBoxString);
                byte[] win1253Bytes = win1253.GetBytes(textBoxString);
    
                foreach (byte b in unicodeBytes)
                {
                    Console.Write($"[{b:x2}]");
                }
    
                Console.WriteLine("\n------------------------");
    
                foreach (byte b in win1253Bytes)
                {
                    Console.Write($"[{b:x2}]");
                }
    
                Console.WriteLine("\n------------------------");
            }
        }
    }
    

    Result

    enter image description here


1 additional answer

Sort by: Most helpful
  1. SurferOnWww 3,201 Reputation points
    2024-11-07T06:13:24.1166667+00:00

    This answer has been deleted because of duplication.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.