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

后端 未结 2 1558
逝去的感伤
逝去的感伤 2021-02-20 14:17

I need help with GDAL. The string value with Chinese symbols is not readed/saved correctly (C#).

For SAVING grid value we using:
private static extern void GDALR

相关标签:
2条回答
  • 2021-02-20 14:48

    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;
        }
    
    0 讨论(0)
  • 2021-02-20 14:48

    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.

    0 讨论(0)
提交回复
热议问题