从sybase数据库里面读出的是utf8编码格式,用delphi自带的uft8tounicode函数会出现这样的问题:字符结束处或者中英文字符的边界处出现丢字符情况,C#中也出现了类似的问题,但是给以用过utf8类自带的读取字节流的方法转化出来,delphi一直找不到,各位大侠有什么好办法吗?
解决方案 »
- 一表多字段查询如何写呢?
- dbgrid显示数据问题
- 为什么我的子窗体显示的时候总是靠在父窗体的左上角? 如何指定子窗体显示的位置?
- 在cxgrid 中把某一行显示为序号的,该怎么做啊 各位大哥大姐帮帮忙啊
- 如何把我的程序改为NT服务程序的自动运行的程序?请高手指导
- clientdataset过滤问题:怎样过滤bool值的字段???
- ADOConnection连接串写到Dll中
- showmodal方法的问题
- 请问下面这些delphi的DLL声明怎么转换成VB的DLL声明,谢谢
- 请问flash控件是不是在help里查不到的??? 我是新手,该怎么办呢???
- 有office outlook编程的进来挑战一下!!!
- 连接MYSQL遇到的问题,希望和大家讨论(不仅仅是针对这个问题)
--------------
function Utf8Decode(const S: UTF8String): WideString; function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: Cardinal;
Source: PAnsiChar; SourceBytes: Cardinal): Integer;
var
i, count: Cardinal;
c: Byte;
wc: Cardinal;
begin
if Source = nil then
begin
Result := 0;
Exit;
end;
Result := -1;
count := 0;
i := 0;
if Dest <> nil then
begin
while (i < SourceBytes) and (count < MaxDestChars) do
begin
wc := Cardinal(Source[i]);
Inc(i);
if (wc and $80) <> 0 then
begin
if i >= SourceBytes then Exit; // incomplete multibyte char
wc := wc and $3F;
if (wc and $20) <> 0 then
begin
c := Byte(Source[i]);
Inc(i);
if (c and $C0) <> $80 then Exit; // malformed trail byte or out of range char
if i >= SourceBytes then Exit; // incomplete multibyte char
wc := (wc shl 6) or (c and $3F);
end;
c := Byte(Source[i]);
Inc(i);
if (c and $C0) <> $80 then Exit; // malformed trail byte Dest[count] := WideChar((wc shl 6) or (c and $3F));
end
else
Dest[count] := WideChar(wc);
Inc(count);
end;
if count >= MaxDestChars then count := MaxDestChars-1;
Dest[count] := #0;
end
else
begin
while (i < SourceBytes) do
begin
c := Byte(Source[i]);
Inc(i);
if (c and $80) <> 0 then
begin
if i >= SourceBytes then Exit; // incomplete multibyte char
c := c and $3F;
if (c and $20) <> 0 then
begin
c := Byte(Source[i]);
Inc(i);
if (c and $C0) <> $80 then Exit; // malformed trail byte or out of range char
if i >= SourceBytes then Exit; // incomplete multibyte char
end;
c := Byte(Source[i]);
Inc(i);
if (c and $C0) <> $80 then Exit; // malformed trail byte
end;
Inc(count);
end;
end;
Result := count + 1;
end;var
L: Integer;
Temp: WideString;
begin
Result := '';
if S = '' then Exit;
SetLength(Temp, Length(S)); L := Utf8ToUnicode(PWideChar(Temp), Length(Temp)+1, PAnsiChar(S), Length(S));
if L > 0 then
SetLength(Temp, L-1)
else
Temp := '';
Result := Temp;
end;