从sybase数据库里面读出的是utf8编码格式,用delphi自带的uft8tounicode函数会出现这样的问题:字符结束处或者中英文字符的边界处出现丢字符情况,C#中也出现了类似的问题,但是给以用过utf8类自带的读取字节流的方法转化出来,delphi一直找不到,各位大侠有什么好办法吗?
解决方案 »
- Delphi中如何在edit中显示查询到的列表中某一项数值?
- fastreport 3.X 如何控制每页固定显示30行!如果超过30行自动转到下页
- SQLSERVER 如何进行双机备份请教各位大侠!
- 用Delphi编的三层结构如何配置连接
- 关于ID3
- 关于dcu文件的问题(在线等待)
- 我只有这么多分了,但是希望大虾们能够解决我的问题:"如何将数据库中的数据赋值给一个变量?
- [讨论]:关于DELPHI中ADO缓存更新技术的并发处理
- 使用MSAGENT时,怎么才能在屏幕上出现两个一样的人物?比如两个MERLIN???
- 怎样使一个程序不显示在状态栏也不显示在图标栏?
- 有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;