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
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;
}