在处理IMAP4时,发现从服务器发送过来的消息,是用UTF7进行编码的。
字符串形如
&XfJT0ZABkK5O9g-
这是一种UTF-7的编码,在进行处理时,需要将其首字母&改为+
变成
+XfJT0ZABkK5O9g-
然后进行转换,转换的代码如下。
1、C++代码
2、DELPHI代码
字符串形如
&XfJT0ZABkK5O9g-
这是一种UTF-7的编码,在进行处理时,需要将其首字母&改为+
变成
+XfJT0ZABkK5O9g-
然后进行转换,转换的代码如下。
1、C++代码
CString Convert(CString str, int sourceCodepage, int targetCodepage)
{
int len = str.GetLength();
int unicodeLen = MultiByteToWideChar(sourceCodepage, 0, str, -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage, 0, str, -1, (LPWSTR)pUnicode, unicodeLen);//61 00 11 62 62 00 00 00
BYTE *pTargetData = NULL;
int targetLen = WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1, (char *)pTargetData, 0, NULL, NULL);
pTargetData = new BYTE[targetLen + 1];
memset(pTargetData, 0, targetLen + 1);
WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1, (char *)pTargetData, targetLen, NULL, NULL);//61 e6 88 91 62 00
CString rt;
rt.Format("%s", pTargetData);
delete pUnicode;
delete pTargetData;
return rt;
}
{
int len = str.GetLength();
int unicodeLen = MultiByteToWideChar(sourceCodepage, 0, str, -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(sourceCodepage, 0, str, -1, (LPWSTR)pUnicode, unicodeLen);//61 00 11 62 62 00 00 00
BYTE *pTargetData = NULL;
int targetLen = WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1, (char *)pTargetData, 0, NULL, NULL);
pTargetData = new BYTE[targetLen + 1];
memset(pTargetData, 0, targetLen + 1);
WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1, (char *)pTargetData, targetLen, NULL, NULL);//61 e6 88 91 62 00
CString rt;
rt.Format("%s", pTargetData);
delete pUnicode;
delete pTargetData;
return rt;
}
2、DELPHI代码
function Convert(str: string; sourceCodepage: Cardinal; targetCodepage: Cardinal): string;
var
Len: Integer;
UnicodeLen: Integer;
pUnicode: PWideChar;
pTargetData: PChar;
targetLen: Integer;
begin
Len := Length(str);
UnicodeLen := MultiByteToWideChar(sourceCodepage, 0, PAnsiChar(str), -1, nil, 0);
GetMem(pUnicode, (UnicodeLen + 1) * SizeOf(WideChar));
ZeroMemory(pUnicode, (UnicodeLen + 1) * SizeOf(WideChar));
MultiByteToWideChar(sourceCodepage, 0, PAnsiChar(str), -1, pUnicode, unicodeLen);//61 00 11 62 62 00 00 00
pTargetData := nil;
targetLen := WideCharToMultiByte(targetCodepage, 0, PWideChar(pUnicode), -1, pTargetData, 0, nil, nil);
GetMem(pTargetData, targetLen + 1);
ZeroMemory(pTargetData, targetLen + 1);
WideCharToMultiByte(targetCodepage, 0, PWideChar(pUnicode), -1, pTargetData, targetLen, nil, nil);//61 e6 88 91 62 00
Result := Copy(PTargetData, 1, Length(pTargetData));
FreeMem(pTargetData);
FreeMem(pUnicode);
end;
var
Len: Integer;
UnicodeLen: Integer;
pUnicode: PWideChar;
pTargetData: PChar;
targetLen: Integer;
begin
Len := Length(str);
UnicodeLen := MultiByteToWideChar(sourceCodepage, 0, PAnsiChar(str), -1, nil, 0);
GetMem(pUnicode, (UnicodeLen + 1) * SizeOf(WideChar));
ZeroMemory(pUnicode, (UnicodeLen + 1) * SizeOf(WideChar));
MultiByteToWideChar(sourceCodepage, 0, PAnsiChar(str), -1, pUnicode, unicodeLen);//61 00 11 62 62 00 00 00
pTargetData := nil;
targetLen := WideCharToMultiByte(targetCodepage, 0, PWideChar(pUnicode), -1, pTargetData, 0, nil, nil);
GetMem(pTargetData, targetLen + 1);
ZeroMemory(pTargetData, targetLen + 1);
WideCharToMultiByte(targetCodepage, 0, PWideChar(pUnicode), -1, pTargetData, targetLen, nil, nil);//61 e6 88 91 62 00
Result := Copy(PTargetData, 1, Length(pTargetData));
FreeMem(pTargetData);
FreeMem(pUnicode);
end;