C# bindings: Utf-8 characters and GetFieldAsString() errors
I am currently developing a C#.Net desktop application with GDAL/OGR CSharp interface (v1.4.0). I am trying to open and read ESRI shapefiles encoded in UTF-8 that contains special characters as â or ï.
Expected behavior and actual behavior.
When I try to get the content of the fields with GetFieldAsString(“Nam”), the expected strings should be :

However, what I get is:

This photo is taken while debbuging, to be sure that GetFieldAsString("Nam") was returning a string with some missing final characters. It seems that there is an encoding problem.
Here you can find the shapefile:
Steps to reproduce the problem.
Just try to GetFieldAsString on the Nam field in order to retrieve the string without missing any final character. My goal was to copy that string onto another new shapefile, but I am struggling to have the same String.
It seems that there is an encoding problem, because every feature with special characters turns out to delete some final characters. ¿Is it a library problem? ¿How can I know that GetFieldAsString() is taking the data correctly and maybe .NET is losing characters by encoding them to UTF-16?
Operating system
Windows 10 Pro 64bits (10.0, compilation 17134)
GDAL version and provenance
GDAL/OGR C# interface 1.4.0
Thank you all.
esri file gdb also fail GetFieldAsString when value is non english string, and file path has same problem
Since I wasn´t able to find the way in order to fix it, I created a custom dll from gdal/ogr C++ just for doing GetFieldAsString() . I really think that the problem is located inside the C# wrapper :D
@IgnacioAscondoGomez I've checked with the recent OGR/C# and I seem to get correct result. You might also try the ogrinfo console c# example from http://download.gisinternals.com/sdk/downloads/release-1911-gdal-mapserver.zip (open SDKShell.bat and cd into bin\gdal\csharp, then "ogrinfo [path to shapefile]\ADB170.shp")
I have just checked out the content of that shapefile with ogrinfo and I still experience the same problem.
Here you can see that the NAM field is Drâ' Berr instead of Drâ' Berrani, losing "ani" because of the special characters.

Is it a problem from the 1.4.0 version that has been fixed in the 1.5.0 version?
Hi,
I've got the same problem but it only appears with a Mono environment. With .Net ogrinfo displays the correct field values but when executed with Mono the values are truncated if they contain special characters.
Régis.
C#'s GetFieldAsString failed to properly decode Hebrew text (Windows-1255 encoding). Instead of the correct text, I was getting a string full of replacement characters (�).
My project targets .NET 8.0. I'm using nugets:
- GDAL 3.10.0
- GDAL.Native 3.10.0
I initially considered using GetFieldAsBinary as a workaround to decode the text manually. However, I found a different solution by creating a C++/CLI bridge project that uses GDAL directly:
// filepath: GDALOps.h
namespace GDALOps
{
public ref class Ops
{
public:
static String^ ProcessFeatureFromManaged(IntPtr featurePtr, int i)
{
OGRFeature* poFeature = static_cast<OGRFeature*>(featurePtr.ToPointer());
auto result = poFeature->GetFieldAsString(i);
return gcnew String(result);
}
};
}
Interestingly, this approach works perfectly for handling the Hebrew text encoding.