beta高手帮忙,文本内容写为十六进制的BIN文件。。。对TMemoryStream,TFileStream流熟悉的高手请帮忙,谢谢(100分)

小小K

Unregistered / Unconfirmed
GUEST, unregistred user!
现有一个文本文件( kk.txt 大小 250K ),内容为:
^^^^^^^^
F1 D3 A8 00 F3 00 12 33 F1 D3 A8 00 F3 00 12 33
F2 44 44 00 F3 00 12 33 F1 D3 A8 00 88 00 12 33
F3 D3 55 00 F3 00 12 33 F1 D3 88 99 16 30 88 99
……

我需要把他写成十六进制文件如 kk.bin


高手请再帮忙一下,谢谢。

BETA这是我上次的问的问题:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1279031
 
没看明白,BIN与TXT有什么区别?
 

一个字符一个字符转为Byte的方式,不过这样应该会很慢。
 
以下是BCB写的函数,你看行否?
///////////////////////////////////////////////////
///str2hex函数功能:16进制字符串还原
/// AnsiString str:要转换的字符串
// char * buff :存放转换后的数据
// 返回值:int 数据长度。
////////////////////////////////////////////////////
int __fastcall TForm1::str2hex(AnsiString str,char * buff)
{
AnsiString StrTable;
StrTable = "<00><01><02><03><04><05><06><07><08><09><0A><0B><0C><0D><0E><0F>";
StrTable+= "<10><11><12><13><14><15><16><17><18><19><1A><1B><1C><1D><1E><1F>";
StrTable+= "<20><21><22><23><24><25><26><27><28><29><2A><2B><2C><2D><2E><2F>";
StrTable+= "<30><31><32><33><34><35><36><37><38><39><3A><3B><3C><3D><3E><3F>";
StrTable+= "<40><41><42><43><44><45><46><47><48><49><4A><4B><4C><4D><4E><4F>";
StrTable+= "<50><51><52><53><54><55><56><57><58><59><5A><5B><5C><5D><5E><5F>";
StrTable+= "<60><61><62><63><64><65><66><67><68><69><6A><6B><6C><6D><6E><6F>";
StrTable+= "<70><71><72><73><74><75><76><77><78><79><7A><7B><7C><7D><7E><7F>";
StrTable+= "<80><81><82><83><84><85><86><87><88><89><8A><8B><8C><8D><8E><8F>";
StrTable+= "<90><91><92><93><94><95><96><97><98><99><9A><9B><9C><9D><9E><9F>";
StrTable+= "<A0><A1><A2><A3><A4><A5><A6><A7><A8><A9><AA><AB><AC><AD><AE><AF>";
StrTable+= "<B0><B1><B2><B3><B4><B5><B6><B7><B8><B9><BA><BB><BC><BD><BE><BF>";
StrTable+= "<C0><C1><C2><C3><C4><C5><C6><C7><C8><C9><CA><CB><CC><CD><CE><CF>";
StrTable+= "<D0><D1><D2><D3><D4><D5><D6><D7><D8><D9><DA><DB><DC><DD><DE><DF>";
StrTable+= "<E0><E1><E2><E3><E4><E5><E6><E7><E8><E9><EA><EB><EC><ED><EE><EF>";
StrTable+= "<F0><F1><F2><F3><F4><F5><F6><F7><F8><F9><FA><FB><FC><FD><FE><FF>";

str=str.UpperCase();
int iPos;
int num;
int iLen=str.Length();
int nPos=0;
AnsiString a_str="";

for(int i=0;i<iLen;i++){
if(str.c_str()>='0' && str.c_str()<='F' && str.c_str()!='@')
a_str+=str.c_str();

if(a_str.Length()==2){
iPos=StrTable.Pos("<"+a_str+">");
num=((iPos+3) / 4)-1;
buff[nPos]=num;
nPos++;
a_str="";
}
}
return nPos;
}
 
你的问题需要高手吗?还是基本功!

十六进制文本转化为文件流。
procedure TForm1.Button1Click(Sender: TObject);
var
SL,SL1:TStrings;
i,j:integer;
c:char;
fs:Tfilestream;
begin
SL:=TStringList.create;
SL1:=TStringList.create;
SL.LoadFromFile('D:/Temp/aaa.txt');
fs:=TFileStream.Create('d:/temp/kk.bin',fmCreate);
for i:=0 to SL.Count-1 do
begin
SL1.CommaText :=SL.Strings;
for j:=0 to SL1.Count-1 do
begin
c:=char(strtoint('$'+SL1.strings[j]));
fs.Write(c,1);
end
end;
fs.free;
SL.free;
SL1.free;
end;

你的另一贴没有讲清楚,我只能去猜。但我相你会从这儿受到启发的。
 
[:D]哈,高手厉害,好感谢你JSXJD。[:D]
 
顶部