convert a string to latin1 in csharp

To convert a string to Latin1 encoding in C#, you can use the Encoding class along with the GetEncoding method that takes the code page number of Latin1 encoding (ISO 8859-1) as an argument.

main.cs
        Here's an example:

        ```csharp
        string inputString = "This is a sample string.";
        byte[] latin1Bytes = Encoding.GetEncoding(28591).GetBytes(inputString);
        string latin1String = Encoding.GetEncoding(28591).GetString(latin1Bytes);
        Console.WriteLine(latin1String);
        ```

        In the above code, `GetEncoding(28591)` returns the Latin1 encoding object. We first convert the input string to bytes using the Latin1 encoding, and then use the same Latin1 encoding to create a new string from the bytes.

        The output of this code will be:

        ```
        This is a sample string.
        ```
650 chars
17 lines

related categories

gistlibby LogSnag