字符集WideCharToMultiByte
GDAL C#封装对中文字符转换过程中存在问题。
C++封装一个Win32 DLL,采用Unicode字符集。使用标准头文件。
https://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx
class CodePageHelper
{
public:
CodePageHelper(void);
~CodePageHelper(void);
static wchar_t* ANSIToUnicode( const char* str );
static char* UnicodeToANSI( const wchar_t* str );
static wchar_t* UTF8ToUnicode( const char* str );
static char* UnicodeToUTF8( const wchar_t* str );
};
CodePageHelper.h
#include "StdAfx.h"
#include "CodePageHelper.h" CodePageHelper::CodePageHelper(void)
{
} CodePageHelper::~CodePageHelper(void)
{
} // ANSI to Unicode
wchar_t* CodePageHelper:: ANSIToUnicode( const char* str )
{
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
,
str,
-,
NULL,
);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+];
memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
,
str,
-,
(LPWSTR)pUnicode,
unicodeLen );
wchar_t* rt;
rt = ( wchar_t* )pUnicode;
//delete pUnicode; return rt;
}
// Unicode to ANSI
char* CodePageHelper:: UnicodeToANSI( const wchar_t* str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
,
str,
-,
NULL,
,
NULL,
NULL );
pElementText = new char[iTextLen + ];
memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
::WideCharToMultiByte( CP_ACP,
,
str,
-,
pElementText,
iTextLen,
NULL,
NULL );
char* strText;
strText = pElementText;
//delete[] pElementText;
return strText;
}
// UTF-8 to Unicode
wchar_t* CodePageHelper::UTF8ToUnicode( const char* str )
{
int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
,
str,
-,
NULL,
);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+];
memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
::MultiByteToWideChar( CP_UTF8,
,
str,
-,
(LPWSTR)pUnicode,
unicodeLen );
wchar_t* rt;
rt = ( wchar_t* )pUnicode;
//delete pUnicode; return rt;
}
// Unicode to UTF-8 char* CodePageHelper::UnicodeToUTF8( const wchar_t* str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8,
,
str,
-,
NULL,
,
NULL,
NULL );
pElementText = new char[iTextLen + ];
memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
::WideCharToMultiByte( CP_UTF8,
,
str,
-,
pElementText,
iTextLen,
NULL,
NULL );
char* strText;
strText = pElementText;
//delete[] pElementText;
return strText;
}
CodePageHelper
#ifdef DEMIMP_EXPORTS
#define CPL_DLL __declspec(dllexport)
#else
#define CPL_DLL __declspec(dllimport)
#endif #ifndef CPL_DISABLE_STDCALL
# define CPL_STDCALL __stdcall
#endif extern "C"
{
HANDLE CPL_DLL WINAPI GetMetaData(LPWSTR filepath);
};
GDALRaster.h
HANDLE CPL_DLL WINAPI GetMetaData(LPWSTR filepath)
{
//char* file=CodePageHelper::UnicodeToUTF8((const wchar_t*)filepath); char* file1=CodePageHelper::UnicodeToANSI((const wchar_t*)filepath);
//const wchar_t* file2=filepath;
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
GDALDataset *pDSrc = (GDALDataset *)GDALOpen(file1, GA_ReadOnly);
if (pDSrc == NULL)
{
return ;
}
char** metadata=pDSrc->GetMetadata("");
return metadata;
}
C# P/Invoke调用:
[DllImport("GDALRaster.dll", EntryPoint = "GetMetaData", CharSet = CharSet.Unicode)]
private static extern IntPtr CSharp_GetMetadata([In, MarshalAs(UnmanagedType.LPWStr)]string filepath);
解析字符串:
public static string[] GetMetaData(string filePath)
{
IntPtr cPtr = CSharp_GetMetadata(filePath);
if (cPtr == IntPtr.Zero) throw new Exception("打开失败"); IntPtr objPtr;
int count = ;
if (cPtr != IntPtr.Zero)
{
while (Marshal.ReadIntPtr(cPtr, count * IntPtr.Size) != IntPtr.Zero)
++count;
}
string[] ret = new string[count];
if (count > )
{
for (int cx = ; cx < count; cx++)
{
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)));
ret[cx] = (objPtr == IntPtr.Zero) ? null : System.Runtime.InteropServices.Marshal.PtrToStringAnsi(objPtr);
}
}
return ret;
//double[] temp = new double[xsize * ysize];
//Marshal.Copy(pData, temp, 0, xsize * ysize);
//FreeData(pData);
//return temp; }
字符集WideCharToMultiByte的更多相关文章
- 关于多字节、宽字节、WideCharToMultiByte和MultiByteToWideChar函数的详解
所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码. 而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE. **************************** ...
- Unicode字符集下CString与char *转换 (解决中文乱码等)(转)
UniCode 下 CString 转 char* 的方法的文章有很多,但是大部分都是在互相转载,看了那么多资料,仍然没有解决乱码的问题,后来从一个论坛的一条回复里面找到了正确的方法,特此拿出来与大家 ...
- MULTIBYTETOWIDECHAR的与WIDECHARTOMULTIBYTE的参数详解及相互转换
第一个就是宽字符到多字节字符转换函数,函数原型如下: int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideChar ...
- Windows字符集的统一与转换
以前也零零散散看过一些字符编码的问题,今天看来这边博客,感觉很多东西都总结在里面,非常值得学习! 一.字符集的历史渊源 在Windows编程时经常会遇到编码转换的问题,一直以来让刚接触的人摸不着头脑. ...
- 函数WideCharToMultiByte() 详解
函数原型: int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPWSTR lpWideCharStr, int cchWideChar, ...
- 字符集转换: Unicode - Ansi
字符集转换: Unicode - Ansi string UnicodeToAnsi ( const wstring& wstrSrc ) { /*!< 分配目标空间, 一个16位Uni ...
- MultiByteToWideChar和WideCharToMultiByte用法详解
今天写ini文件的时候发现的问题: TCHAR temp[]; //strcpy_s(temp, request.newVersion); MultiByteToWideChar(CP_ACP, , ...
- 转:Unicode字符集和多字节字符集关系
原文地址: http://my.oschina.net/alphajay/blog/5691 unicode.ucs-2.ucs-4.utf-16.utf-32.utf-8 http://stallm ...
- Unicode字符集下CString与char *相互转换
经常遇到CString转换char*时只返回第一个字符.原因是因为在Unicode字符集下CString会以Unicode的形式来保存数据,强制类型转换只会返回第一个字符.所以直接转换在基于MBCS的 ...
随机推荐
- NBUT 1010 魔法少女(DP)
[1010] 魔法少女 时间限制: 1000 ms 内存限制: 65535 K 问题描述 前些时间虚渊玄的巨献小圆着实火了一把. 在黑长直(小炎)往上爬楼去对抗魔女之夜时,她遇到了一个问题想请你帮忙. ...
- 《你不知道的JavaScript》读书笔记(一)作用域
名词 引擎:从头到尾负责整个 JavaScript 程序的 编译 及 执行 过程. 编译器:负责 语法分析 及 代码生成. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套 ...
- PHP 错误与异常 笔记与总结(3)PHP 配置文件(php.ini)中与错误相关的选项 与 设置错误级别
[PHP 配置文件中与错误相关的选项 ] 选项 描述 error_reporting 设置错误报告的级别 display_errors 是否显示错误 log_errors 设置是否将错误信息记录到日志 ...
- Javascript 笔记与总结(1-4)this
js 中函数的 4 种调用方式: ① 作为普通函数来调用,this 的值指向 window,准确地说,this 为 null,被解释成为 window.在 ECMAScript5 标准中,如果 thi ...
- adb & adb shell 相关命令
在Mac上配置adb命令 在Mac OS中使用adb命令时,应进行变量配置,步骤如下: 一.终端中输入 cd ~ 二.输入touch .bash_profile 回车 touch:如果没有,则创建文件 ...
- UIButton 去除按下效果(阴影)
[btn setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];btn.adjustsI ...
- mysql线程缓存和表缓存
一.线程缓存1.thread_cache_size定义了线程缓冲中的数量.每个缓存中的线程通常消耗256kb内存2.Threads_cached,可以看到已经建立的线程二.表缓存(table_cach ...
- rgb转灰度 RGB To Gray php Adobe RGB (1998) [gamma=2.20]
<?php /** * Date: 2016/10/24 * Time: 0:52 */ // Gray = (R^2.2 * 0.2973 + G^2.2 * 0.6274 + B^2.2 * ...
- html5之canvas初解
<canvas> 元素本身并没有绘制能力(它仅仅是图形的容器) - 必须使用脚本来完成实际的绘图任务. getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属 ...
- WCF 无法激活服务,因为它不支持 ASP.NET 兼容性。已为此应用程序启用了 ASP.NET 兼容性
无法激活服务,因为它不支持 ASP.NET 兼容性.已为此应用程序启用了 ASP.NET 兼容性.请在 web.config 中关闭 ASP.NET 兼容性模式,或将 AspNetCompatibil ...