Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Char.ConvertToUtf32(String, Int32) trong C#

Phương thức Char.ConvertToUtf32(String, Int32) trong C# chuyển đổi giá trị của một ký tự được mã hóa UTF-16 hoặc cặp surrogate tại vị trí xác định trong chuỗi thành một điểm mã Unicode (Unicode code point).

Cú pháp

public static int ConvertToUtf32(string str, int index);

str là chuỗi chứa ký tự hoặc cặp surrogate. index là vị trí của ký tự/cặp surrogate trong chuỗi.

Ví dụ 1

using System;
public class Demo {
    public static void Main() {
        int utf = 0x046;
        string str = Char.ConvertFromUtf32(utf);
        Console.WriteLine("Giá trị cuối = " + str);
        int res = Char.ConvertToUtf32(str, 0);
        Console.WriteLine("Giá trị thực = 0x{0:X}", res);
    }
}

Kết quả

Giá trị cuối = F
Giá trị thực = 0x46

Ví dụ 2

using System;
public class Demo {
    public static void Main() {
        int utf = 0x057;
        string str = Char.ConvertFromUtf32(utf);
        Console.WriteLine("Giá trị cuối = " + str);
        int res = Char.ConvertToUtf32(str, 0);
        Console.WriteLine("Giá trị thực = 0x{0:X}", res);
    }
}

Kết quả

Giá trị cuối = W
Giá trị thực = 0x57