Utf8Decode不能正确解码UTF8字符串的问题
使用Utf8Decode处理UTF-8字符串时,有时候正常,有时候返回空值,请问是怎么回事?
例如用Utf8Decode处理http://gz.ganji.com/zpshichangyingxiao/ 这个网页的源代码时就是返回空值。
[解决办法]
function UTF8Decode(Data: AnsiString): WideString;begin SetLength(Result, Length(Data)); SetLength(Data,MultiByteToWideChar(CP_UTF8,0,PAnsiChar(data),Length(Data),PWideChar(Result),Length(Data)));end;
[解决办法]
这么久还没搞定?查个msdn这么费劲么……
function DecodeUtf8Str(const S: UTF8String): WideString;var lenSrc, lenDst : Integer;begin lenSrc := Length(S); if(lenSrc=0)then Exit; lenDst := MultiByteToWideChar(CP_UTF8, 0, Pointer(S), lenSrc, nil, 0); SetLength(Result, lenDst); MultiByteToWideChar(CP_UTF8, 0, Pointer(S), lenSrc, Pointer(Result), lenDst);end;