首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

如何才能将string分割到byte数组

2012-02-03 
怎么才能将string分割到byte数组比如string: 25000700000008000000D504000000000000怎么才能将string按空

怎么才能将string分割到byte数组
比如string:= '25   00   07   00   00   00   08   00   00   00   D5   04   00   00   00   00   00   00 '
怎么才能将string按空格分割到byte数组中呢?
谢谢

[解决办法]

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;

热点排行