Csharp/C Sharp/File Stream/Encode

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Convert Byte array to Base64 Char Array

using System;
using System.IO;
class Test {
    public static void Main() {
        // The size of the char[] must 
        // be at least 4/3 the size of the source byte[] and must be 
        // divisible by 4.
        byte[] data = { 0x01, 0x02, 0x5A, 0xFF, 0x0, 0xF0, 0x4D, 0x62, 0x78,  
            0xD2, 0xC5, 0xA1, 0x5A, 0xD6, 0x0C, 0xA9, 0xA6, 0x63, 0x3D, 0xC2, 
            0xD5, 0x0F, 0xCC, 0x01, 0xF2, 0x0C};
        char[] base64data =  new char[(int)(Math.Ceiling((double)data.Length / 3) * 4)];
        Console.WriteLine("\nByte array encoding/decoding");
        Convert.ToBase64CharArray(data, 0, data.Length, base64data, 0);
        Console.WriteLine(new String(base64data));
    }
}


Encoding.ASCII.GetString

 
using System;
using System.Text;
class StringEncodingApp {
    static void Main(string[] args) {
        byte[] ba = new byte[] { 72, 101, 108, 108, 111 };
        string s = Encoding.ASCII.GetString(ba);
        Console.WriteLine(s);
    }
}


Encoding.BigEndianUnicode.GetString

 
using System;
using System.Text;
class StringEncodingApp {
    static void Main(string[] args) {
        byte[] bb = new byte[] { 0, 72, 0, 101, 0, 108, 0, 108, 0, 111 };
        string t = Encoding.BigEndianUnicode.GetString(bb);
        Console.WriteLine(t);
    }
}


Encoding in action

using System;
using System.IO;
using System.Text;
class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = \u03A0r^2";
            // Convert the UTF-16 encoded source string to UTF-8 and ASCII.
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
            
            // Write the UTF-8 and ASCII encoded byte arrays. 
            output.WriteLine("UTF-8  Bytes: {0}", BitConverter.ToString(utf8String));
            output.WriteLine("ASCII  Bytes: {0}", BitConverter.ToString(asciiString));
            
            
            // Convert UTF-8 and ASCII encoded bytes back to UTF-16 encoded  
            // string and write.
            output.WriteLine("UTF-8  Text : {0}", Encoding.UTF8.GetString(utf8String));
            output.WriteLine("ASCII  Text : {0}", Encoding.ASCII.GetString(asciiString));
            Console.WriteLine(Encoding.UTF8.GetString(utf8String));
            Console.WriteLine(Encoding.ASCII.GetString(asciiString));
        }
    }
}


Encoding: Unicode ASCII GetBytes GetString

 
using System;
using System.IO;
using System.Text;
class MainClass
{
    public static void Main()
    {
        using (StreamWriter output = new StreamWriter("output.txt"))
        {
            string srcString = "Area = \u03A0r^2";
            output.WriteLine("Source Text : " + srcString);
            byte[] utf16String = Encoding.Unicode.GetBytes(srcString);
            output.WriteLine("UTF-16 Bytes: {0}",BitConverter.ToString(utf16String)); 
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
            output.WriteLine("UTF-8 Bytes: {0}",
            BitConverter.ToString(utf8String));
            output.WriteLine("ASCII Bytes: {0}",
            BitConverter.ToString(asciiString));
            output.WriteLine("UTF-8 Text : {0}",Encoding.UTF8.GetString(utf8String));
            output.WriteLine("ASCII Text : {0}",Encoding.ASCII.GetString(asciiString));
        }
    }
}


Encoding.URF8/Unicode/UTF32/.GetBytes

 
using System;
public class MainClass {
    public static void Main() {
        byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes("0123456789");
        byte[] utf16Bytes = System.Text.Encoding.Unicode.GetBytes("0123456789");
        byte[] utf32Bytes = System.Text.Encoding.UTF32.GetBytes("0123456789");
        Console.WriteLine(utf8Bytes.Length);
        Console.WriteLine(utf16Bytes.Length);
        Console.WriteLine(utf32Bytes.Length);
    }
}


Encoding.URF8/Unicode/UTF32/GetString

 
using System;
public class MainClass {
    public static void Main() {
        byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes("0123456789");
        byte[] utf16Bytes = System.Text.Encoding.Unicode.GetBytes("0123456789");
        byte[] utf32Bytes = System.Text.Encoding.UTF32.GetBytes("0123456789");
        string original1 = System.Text.Encoding.UTF8.GetString(utf8Bytes);
        string original2 = System.Text.Encoding.Unicode.GetString(utf16Bytes);
        string original3 = System.Text.Encoding.UTF32.GetString(utf32Bytes);
        Console.WriteLine(original1);
        Console.WriteLine(original2);
        Console.WriteLine(original3);
    }
}