GDAL GDALRATSetValueAsString() how to save Chinese characters (c#)?

こ雲淡風輕ζ 提交于 2019-12-04 01:58:15

GDAL uses UTF-8 encoding internally when working with strings. That means strings must be converted to UTF-8 before passing them to GDAL. The same is valid for GDAL output strings - have to be converted from UTF-8 to local encoding before using.

C# uses UTF-16 strings so conversions to UTF-8 and back must be introduced:

public class EncodingConverter
{
    public static string Utf16ToUtf8(string utf16String)
    {
        byte[] utf16Bytes = Encoding.Unicode.GetBytes(utf16String);
        byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
        return Encoding.Default.GetString(utf8Bytes);
    }

    public static string Utf8ToUtf16(string utf8String)
    {
        byte[] utf8Bytes = Encoding.Default.GetBytes(utf8String);
        byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
        return Encoding.Unicode.GetString(utf16Bytes);
    }
}

Going back to your problem, Japanese characters will be processed correctly if encoding conversion will be applied.

    public void SetValueAsString(int row, int field, string value)
    {
        string utf8Value = EncodingConverter.Utf16ToUtf8(value);
        GDALRATSetValueAsString(GDALRasterAttributeTableH, row, field, utf8Value);
    }

    public string GetValueAsString(int row, int field)
    {
        string value = null;

        var pointer = GDALRATGetValueAsString(GDALRasterAttributeTableH, row, field);
        if (pointer != IntPtr.Zero)
        {
            string utf8Value = Marshal.PtrToStringAnsi(pointer);
            value = EncodingConverter.Utf8ToUtf16(utf8Value);
        }
        return value;
    }

Read this first Specifying a Character Set. Make sure there is a unicode version of GDALRATGetValueAsString. Unicode version ends with a W e.g. GDALRATGetValueAsStringW. ANSI version ends with a A e.g. GDALRATGetValueAsStringA. If you import GDALRATGetValueAsString the charset is auto. It is not clear which version of the function you are referring to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!