把Blob的每一个字节分成两个;
procedure blobToChar(FieldName, FileName: string);
var
strTmp:string;//存放blob数据的数组;
strOut:string;//存放text数据的数组;
length,index:integer;
begin
with TFileStream.create(FileName,fmOpenRead) do
try
length:=size;
setlength(strtmp,size);
setlength(strout,size*2);
read(Pointer(strtmp)^,size);
finally
free;
end;
for index:=1 to length do
begin
strout[index*2-1]:=chr(ord(strtmp[index]) mod 16+ord('a'));
strout[index*2]:=chr(ord(strtmp[index]) div 16+ord('a'));
end;
ADOTableMain.FieldByName(fieldname).AsString:=strOut;
//写到数据库中
end;
procedure TFormMain.SaveFieldToFile(FieldName, FileName: string);
//从text到blob文件
var
strTmp:string;
i,len:integer;
cc:byte;
begin
strTmp:= ADOTableMain.FieldByName(fieldname).AsString;
len:=strlen(pchar(strtmp)) div 2;
with TFileStream.create(FileName,fmCreate) do
try
for i:=1 to len do
begin
cc:=(ord(strtmp[i*2])-ord('a'))*16+(ord(strtmp[i*2-1])-ord('a'));
write(cc,1);
end;
finally
free;
end;
end;
procedure blobToChar(FieldName, FileName: string);
var
strTmp:string;//存放blob数据的数组;
strOut:string;//存放text数据的数组;
length,index:integer;
begin
with TFileStream.create(FileName,fmOpenRead) do
try
length:=size;
setlength(strtmp,size);
setlength(strout,size*2);
read(Pointer(strtmp)^,size);
finally
free;
end;
for index:=1 to length do
begin
strout[index*2-1]:=chr(ord(strtmp[index]) mod 16+ord('a'));
strout[index*2]:=chr(ord(strtmp[index]) div 16+ord('a'));
end;
ADOTableMain.FieldByName(fieldname).AsString:=strOut;
//写到数据库中
end;
procedure TFormMain.SaveFieldToFile(FieldName, FileName: string);
//从text到blob文件
var
strTmp:string;
i,len:integer;
cc:byte;
begin
strTmp:= ADOTableMain.FieldByName(fieldname).AsString;
len:=strlen(pchar(strtmp)) div 2;
with TFileStream.create(FileName,fmCreate) do
try
for i:=1 to len do
begin
cc:=(ord(strtmp[i*2])-ord('a'))*16+(ord(strtmp[i*2-1])-ord('a'));
write(cc,1);
end;
finally
free;
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货