大赵传奇

援引事类,扬搉古今,举要删芜,言辩而理切--QQ276605216

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

首先确认vs 中字符编码:也就是说,vs里 Encoding的 default=GBK

 

 

 

 中  用两个字节存储,十进制 214, 208 = 十六进制  D6 D0

 https://tool.lu/hexconvert/

 转换成 utf-8编码:即为十进制 228 184 173 = 十六进制 e4 b8 ad

 https://www.qqxiuzi.cn/bianma/zifuji.php

 

  

 转换回来,当然还是 中 字

 

 

 

GDAL里

 

 

        [DllImport("gdal303.dll", EntryPoint = "OGR_F_SetFieldString", CallingConvention = CallingConvention.Cdecl)]
        public extern static void OGR_F_SetFieldString(HandleRef handle, int index, string val);
        /// <param name="fe"></param>
        /// <param name="index">索引</param>
        /// <param name="val"></param>
        public static void setFieldString(Feature fe, int index, string val)
        {
            OGR_F_SetFieldString(OSGeo.OGR.Feature.getCPtr(fe), index, val);
        }

 

  

 

 

 

获取字段属性值的乱码解决:

        [DllImport("gdal303.dll", EntryPoint = "OGR_Fld_GetNameRef", CallingConvention = CallingConvention.Cdecl)]
        public extern static System.IntPtr OGR_Fld_GetNameRef(HandleRef handle);


        [DllImport("gdal303.dll", EntryPoint = "OGR_F_GetFieldAsString", CallingConvention = CallingConvention.Cdecl)]
        public extern static System.IntPtr OGR_F_GetFieldAsString(HandleRef handle, int index);

        [DllImport("gdal303.dll", EntryPoint = "OGR_F_GetFieldAsBinary", CallingConvention = CallingConvention.Cdecl)]
        public extern static System.IntPtr OGR_F_GetFieldAsBinary(HandleRef handle, int index, out int byteCount);
        [DllImport("gdal303.dll", EntryPoint = "OGR_F_SetFieldString", CallingConvention = CallingConvention.Cdecl)]
        public extern static void OGR_F_SetFieldString(HandleRef handle, int index, string val);


        /// <param name="fe">当前要素</param>
        /// <param name="index">索引</param>
        /// <returns></returns>
        public static String getFieldAsStr(Feature fe, int index)
        {
            IntPtr pNativeData = OGR_F_GetFieldAsString(OSGeo.OGR.Feature.getCPtr(fe), index);
            if (pNativeData == IntPtr.Zero)
                return null;
            string s = Marshal.PtrToStringAnsi(pNativeData);
            return s;
        }

        /// <param name="fe"></param>
        /// <param name="index">索引</param>
        /// <param name="val"></param>
        public static void setFieldString(Feature fe, int index, string val)
        {
            OGR_F_SetFieldString(OSGeo.OGR.Feature.getCPtr(fe), index, val);
        }

 

var field3 = f.GetFieldAsString(i);//这种直接获取出来的是gbk的乱码


    var field = getFieldAsStr(f, i);
    var bytes2 = System.Text.Encoding.Default.GetBytes(field);
    var sss = System.Text.Encoding.UTF8.GetString(bytes2);//同上。少一些字

用下面方法,实验可以
    //string fieldName = pFieldDefn.GetName();
    //int fieldIndex = pFeatureDefn.GetFieldIndex(fieldName);
    //IntPtr pIntPtr = OGR_F_GetFieldAsString(OSGeo.OGR.Feature.getCPtr(pFeature), fieldIndex);
    int byteCount = 0;
    IntPtr pIntPtr = OGR_F_GetFieldAsBinary(OSGeo.OGR.Feature.getCPtr(f), i, out byteCount);
    byte[] byteArray = new byte[byteCount];
    Marshal.Copy(pIntPtr, byteArray, 0, byteCount);
    string str = System.Text.Encoding.UTF8.GetString(byteArray);

    sb.Append("\"" + key + "\":\"" + str + "\",");

 

posted on 2021-09-29 12:41  赵长青  阅读(252)  评论(0编辑  收藏  举报