比如string:='25 00 07 00 00 00 08 00 00 00 D5 04 00 00 00 00 00 00'
怎么才能将string按空格分割到byte数组中呢?
谢谢
怎么才能将string按空格分割到byte数组中呢?
谢谢
解决方案 »
- 从数据库中取出来内容转成Stream后,不生成xml文件直接放到ADOQuery
- 为什么三行的显示效果是一样的
- fastreport安装问题,请高手看看?
- 小弟刚来,分不多,请问delphi中如何知道DTS包是否执行完毕?请指点
- 一个小问题,却让我郁闷,请大家帮忙.
- 救命呀!再不来要死人呀!
- 如何在ACCESS插入图片,请大家帮忙!!!谢谢!!
- 报表问题
- 请帮个忙
- 一个学过OOA/OOD, UML,Design Pattern, C++, JAVA(一点点),DEPHIL的家伙,该如何继续前进呢?
- 在设计期,如何隐藏基类窗体继承下来的组件?如 ImageList等
- (继续问问题):想要获取和设置没有句柄的组件TEXT内容应该怎么做?
PCh: PChar;
i, j: integer;
begin
PCh := PChar(string);
i := lstrlen(PCh);
for j := 0 to i do
begin
byte[j] := PChar + j;
end;
end;
s = '25 00 07 00 00 00 08 00 00 00 D5 04 00 00 00 00 00 00';
begin
ExtractStrings([' '],[' '],s,Memo1.Lines);
end;Memo1中为值为:
-------------------
25
00
07
00
00
00
08
00
00
00
D5
04
00
00
00
00
00
00
请问还有更好的方法吗?
var
s: tstringlist;
i: integer;
begin
s := tstringlist.Create;
s.DelimitedText:='XXX YYYY RR XX WW';
s.Delimiter:=' ';
for i:=0 to s.Count-1 do
ShowMessage(s.Strings[i]);
s.free;
end;
TStrArray = array of string;function DecodeStr2Array(Source: string; SepChar : char; var Rst: TStrArray): integer;
//根据指定分隔符将字符串分解为独立数据并存入数组
//Source,源字符串
//SepChar,分隔符
//Rst,结果数组
var
Count, CutFrom, Len: word; procedure AddStr(Str: string);
begin
SetLength(Rst, length(Rst) + 1);
Rst[high(Rst)] := Str;
end;
begin
Result := 0;
SetLength(Rst, 0);
len := length(Source);
if len > 0 then
begin
CutFrom := 1; for count := 1 to len do //逐字符扫描字符串,遇到分隔符时则切断串获得数据
if (Source[count] = SepChar) then
begin
AddStr(Copy(Source, CutFrom, count - CutFrom));
CutFrom := count + 1;
if Count = Len then
AddStr('');
end else
if count = len then
begin
AddStr(Copy(Source, CutFrom, count - CutFrom + 1));
Break;
end; Result := length(Rst);
end;
end;
就是怎么才能得到像这样的形式
const temp: array[0..17]of BYTE = ($25,$00,$07,$00,$00,$00,$08,$00,$00,$00,$D5,$04,$00,$00,$00,$00,$00,$00);
function GetByte(const V: Char): Byte;
begin
case V of
'a'..'f':
Result := Ord(V) - Ord('a') + 10;
'A'..'F':
Result := Ord(V) - Ord('A') + 10;
else
Result := Ord(V) - Ord('0');
end;
end;function BinaryDecode(const S: string; output: PByte; outlen: Integer): Integer;
var
R: PByte;
P: PChar;
I: Integer;
begin
Result := Length(S) div 3;
if outlen < Result then
raise Exception.Create('outlen too small'); P := PChar(S);
R := output;
for I := 1 to Result do
begin
R^ := GetByte(P[0]) * 16 + GetByte(P[1]);
Inc(R);
Inc(P, 3);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
len: Integer;
temp: array [0..1000] of Byte;
S: string;
begin
S := '25 00 07 00 00 00 08 00 00 00 D5 04 00 00 00 00 00 00';
Len := BinaryDecode(S, @Temp[0], sizeof(Temp));
ShowMessage(IntToStr(Len));
end;
ByteArray: array of byte;
StrArray: array of string;
Count: integer;
begin
//分解并获取字符串数组
//转换
SetLength(ByteArray, Length(StrArray));
for Count := Low(StrArray) to High(StrArray) do
begin
//可以在这里加入有效性判断和错误处理
ByteArray[Count] := StrToInt(StrArray[Count]);
end;
end;
end;
const
myString = '25 00 07 00 00 00 08 00 00 00 D5 04 00 00 00 00 00 00';
var
tempStr: string;
output: array of byte;
i: integer;
begin
tempStr := StringReplace(myString, ' ', '', [rfReplaceAll, rfIgnoreCase]);
SetLength(output, Length(tempStr) div 2);
HexToBin(PChar(tempStr), PChar(@output[0]), Length(tempStr) div 2);
//上面转化完毕,下面是结果输出
for i := 0 to Length(output) - 1 do
begin
memo1.Lines.Add(IntToStr(output[i]));
end;
end;